首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法通过python获取所有链接

无法通过python获取所有链接
EN

Stack Overflow用户
提问于 2021-11-22 00:00:44
回答 1查看 55关注 0票数 0

我需要src-0,src-1,src-2…附近的链接src-59我怎样才能修复编码?谢谢

代码语言:javascript
复制
!pip install selenium
from selenium import webdriver
import time
import pandas as pd

browser = webdriver.Chrome(executable_path='./chromedriver.exe')
browser.implicitly_wait(5)
browser.get("https://tw.mall.yahoo.com/store/%E5%B1%88%E8%87%A3%E6%B0%8FWatsons:watsons")


# 商品連結

linkPath = "//ul[@class='gridList']/li/a"
product_links = browser.find_elements_by_xpath(linkPath)
print(product_links)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-22 01:37:51

您正在使用的xpath没有突出显示DOM中的任何元素。

像下面这样尝试,希望这些就是你想要提取的链接。

代码语言:javascript
复制
# Imports Required

from selenium import webdriver
from selenium.webdriver.common.by import By

driver.get("https://tw.mall.yahoo.com/store/%E5%B1%88%E8%87%A3%E6%B0%8FWatsons:watsons")

links = driver.find_elements(By.XPATH,"//section[contains(@class,'MainListing__StoreBoothWrap')]/div/div/div/ul/li/a")
print(len(links))

for link in links:
    print(link.get_attribute("href"))
代码语言:javascript
复制
60
https://tw.mall.yahoo.com/item/p0330231079018
https://tw.mall.yahoo.com/item/p0330221397264
https://tw.mall.yahoo.com/item/p0330201617111
...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70059616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档