我正在进行我的第一个编程项目。
我目前正在使用XPATH方法从网页获取链接,然而,当程序运行时,它返回";[无]";。不确定为什么会发生这种情况以及如何解决这个问题。
href链接在html代码中实现,如下所示:
<div class="fixed-recipe-card__info">
<h3 class="fixed-recipe-card__h3">
<a href=“xyz” data-content-provider-id="" data-internal-referrer-link="rotd" class="fixed-recipe-card__title-link ng-isolate-scope" target="_self">
<span class="fixed-recipe-card__title-link">Title</span>≠≠
</a>
</h3>
这是我迄今为止尝试过的代码:
chrome_path = '/Users/name/Downloads/chromedriver'
driver = webdriver.Chrome(executable_path=chrome_path)
driver.get('https://www.website.com/')
driver.implicitly_wait(10)
# scrape for links on the page
elems = driver.find_elements_by_xpath("//h3[@class='fixed-recipe-card__h3']")
#store them in a list
links = []
for elem in elems:
#fetch and store the links
links.append(elem.get_attribute('href'))
#remove the duplicates in list links []
res = [i for n, i in enumerate(links) if i not in links[:n]]
print (str(res))
elems = driver.find_elements_by_xpath("//h3[@class="fixed-receipe-card__h3"]/a')
您正在尝试获取h3标记的href属性,而不是标记。
您也可以使用css选择器:
elems = driver.find_elements_by_css_selector(".fixed-recipe-card__h3 [href]")
links = [elem.get_attribute('href') for elem in elems]