我一直在尝试获取一篇新闻文章主页的所有HREF。最后,我想创造一些东西,让我从所有的新闻文章中找到n个最常用的词。要做到这一点,我想我首先需要HREF,然后一个接一个地点击它们
在这个平台的另一位用户的大量帮助下,我现在得到了以下代码:
from bs4 import BeautifulSoup
from selenium import webdriver
url = 'https://ad.nl'
# launch firefox with your url above
# note that you could change this to some other webdriver (e.g. Chrome)
driver = webdriver.Chrome()
driver.get(url)
# click the "accept cookies" button
btn = driver.find_element_by_name('action')
btn.click()
# grab the html. It'll wait here until the page is finished loading
html = driver.page_source
# parse the html soup
soup = BeautifulSoup(html.lower(), "html.parser")
articles = soup.findAll("article")
for i in articles:
article = driver.find_element_by_class_name('ankeiler')
hrefs = article.find_element_by_css_selector('a').get_attribute('href')
print(hrefs)
driver.quit()
它给出了我认为的第一个href,但不会重复下一个href。它只是给了我第一个href,次数和它迭代的次数一样多。有人知道我是如何让它转到下一个href而不是停留在第一个href上的吗
如果有人对如何进一步完成我的小项目有一些建议,请随意分享,因为我还有很多关于Python和编程的知识需要学习
不要用漂亮的汤,这个怎么样
要在文章中获得所有HREF,您可以执行以下操作:
不过,为了推进项目,下面的吼声可能会有所帮助:
为了改进我之前的回答,我为您的问题写了一个完整的解决方案:
对我来说很好,如果有任何错误,请告诉我
相关问题 更多 >
编程相关推荐