当前位置: 首页 > 知识库问答 >
问题:

Python/Selenium web废料如何从链接中找到隐藏的src值?

韩祯
2023-03-14

废弃链接应该是一个简单的壮举,通常只需获取a标签的< code>src值。

我最近偶然发现这个网站(https://sunteccity.com.sg/promotions ),每个项目的a标签的href值无法找到,但重定向仍然有效。我正在试图找出一种方法来抓取项目及其相应的链接。我的典型python selenium代码如下所示

all_items = bot.find_elements_by_class_name('thumb-img')
for promo in all_items:
    a = promo.find_elements_by_tag_name("a")
    print("a[0]: ", a[0].get_attribute("href"))

但是,我似乎无法检索任何hrefonClick属性,我想知道这是否可能。我注意到我也无法在新选项卡中右键单击打开链接。

有什么方法可以把所有这些项目链接起来吗?

编辑:有什么方法可以找回页面上所有条目的链接?

即。

https://sunteccity.com.sg/promotions/724
https://sunteccity.com.sg/promotions/731
https://sunteccity.com.sg/promotions/751
https://sunteccity.com.sg/promotions/752
https://sunteccity.com.sg/promotions/754
https://sunteccity.com.sg/promotions/280
...

共有2个答案

葛桐
2023-03-14

您使用了错误的定位器。它给你带来了很多不相关的元素。< br >请尝试< code > find _ elements _ by _ class _ name(' thumb-img ')而不是< code > find _ elements _ by _ CSS _ selector('。收藏-页面。thumb-img')所以您的代码将

all_items = bot.find_elements_by_css_selector('.collections-page .thumb-img')
for promo in all_items:
    a = promo.find_elements_by_tag_name("a")
    print("a[0]: ", a[0].get_attribute("href"))

您还可以通过 .collections-page .thumb-img 定位器直接获取所需的链接,以便您的代码可以是:

links = bot.find_elements_by_css_selector('.collections-page .thumb-img a')
for link in links:
    print(link.get_attribute("href"))
佴博实
2023-03-14

通过对Javascript进行逆向工程,该脚本将您带到促销页面(见 https://sunteccity.com.sg/_nuxt/d4b648f.js),该页面为您提供了一种获取所有链接的方法,这些链接基于InventineID。您可以通过在 JS 控制台中运行此命令进行验证,这将为你提供第一个提升:

window.__NUXT__.state.Promotion.promotions[0].HappeningID

基于此,您可以创建一个Python循环来获取所有促销信息:

items = driver.execute_script("return window.__NUXT__.state.Promotion;")
for item in items["promotions"]:
    base = "https://sunteccity.com.sg/promotions/"
    happening_id = str(item["HappeningID"])
    print(base + happening_id)

产生以下输出:

https://sunteccity.com.sg/promotions/724
https://sunteccity.com.sg/promotions/731
https://sunteccity.com.sg/promotions/751
https://sunteccity.com.sg/promotions/752
https://sunteccity.com.sg/promotions/754
https://sunteccity.com.sg/promotions/280
https://sunteccity.com.sg/promotions/764
https://sunteccity.com.sg/promotions/766
https://sunteccity.com.sg/promotions/762
https://sunteccity.com.sg/promotions/767
https://sunteccity.com.sg/promotions/732
https://sunteccity.com.sg/promotions/733
https://sunteccity.com.sg/promotions/735
https://sunteccity.com.sg/promotions/736
https://sunteccity.com.sg/promotions/737
https://sunteccity.com.sg/promotions/738
https://sunteccity.com.sg/promotions/739
https://sunteccity.com.sg/promotions/740
https://sunteccity.com.sg/promotions/741
https://sunteccity.com.sg/promotions/742
https://sunteccity.com.sg/promotions/743
https://sunteccity.com.sg/promotions/744
https://sunteccity.com.sg/promotions/745
https://sunteccity.com.sg/promotions/746
https://sunteccity.com.sg/promotions/747
https://sunteccity.com.sg/promotions/748
https://sunteccity.com.sg/promotions/749
https://sunteccity.com.sg/promotions/750
https://sunteccity.com.sg/promotions/753
https://sunteccity.com.sg/promotions/755
https://sunteccity.com.sg/promotions/756
https://sunteccity.com.sg/promotions/757
https://sunteccity.com.sg/promotions/758
https://sunteccity.com.sg/promotions/759
https://sunteccity.com.sg/promotions/760
https://sunteccity.com.sg/promotions/761
https://sunteccity.com.sg/promotions/763
https://sunteccity.com.sg/promotions/765
https://sunteccity.com.sg/promotions/730
https://sunteccity.com.sg/promotions/734
https://sunteccity.com.sg/promotions/623
 类似资料:
  • 我正在学习网络抓取,因为我从真实的网站抓取真实世界的数据。然而,直到现在我才遇到这种问题。人们通常可以通过右键单击网站的一部分,然后单击检查选项来搜索想要的超文本标记语言源代码。我马上跳到这个例子来解释这个问题。 从上图中,红色标记的span类本来不存在,但是当我把光标放在用户的名字上(甚至没有点击)时,弹出一个该用户的小框,也显示了span类。我最终想要刮取的是嵌入在该span类中的用户配置文件

  • 我想跟踪销售数据从化妆品品牌使用硒,但我有一个困难,找到超链接到下一页。 https://www.gsshop.com/shop/sect/sects.gs?isect=1425746&brandid=143878&lseq=407585 在这个链接中,有3个页面,我可以通过下一个页面,如果我点击页面底部的2或3。 但是,当我检查html代码时,它只返回表单。所以我在“a”标签中找不到任何指向下一

  • 我想这样打印: 谢谢你们的回答伙计们 HTML:

  • 问题内容: 您是否知道在 找不到 图像文件时如何从呈现的HTML页面中隐藏经典的 “未找到图像” 图标? 有使用JavaScript / jQuery / CSS的工作方法吗? 问题答案: 您可以使用JavaScript中的事件来在图像加载失败时采取行动: 在jQuery中(自您询问之后): 或对于所有图像: 如果隐藏图像可能会更改布局,则应使用代替。网络上的许多站点都使用默认的“无图像”图像,当

  • 问题内容: 我在Python中使用Selenium尝试单击鼠标悬停在该下拉菜单上时出现的下拉菜单中的链接。这是下拉菜单信息: 我试图单击“ a href”标签中的“文档”链接。当我将鼠标悬停在菜单上时,代码更改如下: 您可以在第二个ul标签中看到“ visible :: hidden”部分消失了。 我使用以下代码在页面上找到了元素: 如果我尝试使用以下方法单击下载按钮: 我收到一个错误,指出该元素

  • 我有一个隐藏的蓝牙设备,但我知道它的蓝牙MAC地址。 如何使用Android连接到该设备?