简体   繁体   English

使用BeautifulSoup FindAll进行网络爬取

[英]Webscraping with BeautifulSoup FindAll

I want to download the hrefs of the 4 articles right above NEED TO KNOW on the following website: 我想在以下网站上“需要了解”上方下载4篇文章的hrefs:

http://www.marketwatch.com/ http://www.marketwatch.com/

but I cannot identify them uniquely with FindAll. 但是我无法通过FindAll唯一地标识它们。 The following approaches give me the articles, but also a bunch of others, that also fit those criteria. 以下方法为我提供了符合这些条件的文章,但也提供了许多其他文章。

trend_articles  = soup1.findAll("a", {"class": "link"})
href= article.a["href"]

trend_articles  = soup1.findAll("div", {"class": "content--secondary"})
href= article.a["href"]

Does someone have a suggestion, how I can get those 4, and only those 4 articles? 有人有建议吗,我怎么才能得到那4条,只有那4条?

It seems works for me: 看来对我有用:

from bs4 import BeautifulSoup
import requests

page = requests.get("http://www.marketwatch.com/").content
soup = BeautifulSoup(page, 'lxml')
header_secondare = soup.find('header', {'class': 'header--secondary'})
trend_articles = header_secondare.find_next_siblings('div', {'class': 'group group--list '})[0].findAll('a')

trend_articles = [article.contents[0] for article in trend_articles]
print(trend_articles)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM