第四课 Python爬虫简单爬取新浪新闻列表

来源:互联网 发布:怎样做网络推广赚钱 编辑:程序博客网 时间:2024/05/29 10:39

新闻列表页网页结构:


输出h2:

res = requests.get('http://news.sina.com.cn/china/')

res.encoding = 'utf-8'

soup = BeautifulSoup(res.text,'html.parser')

for news in soup.select('.news-item'):

if( len(news.select('h2')) >0 ):

print(news.select('h2')[0])



.text


如法炮制:

获取新闻时间标题超链接

res = requests.get('http://news.sina.com.cn/china/')

res.encoding = 'utf-8'

soup = BeautifulSoup(res.text,'html.parser')

for news in soup.select('.news-item'):

if( len(news.select('h2')) >0 ):

h2 = news.select('h2')[0].text

time = news.select('.time')[0].text

a = news.select('a')[0]['href']

print(time,h2,a)



注:本文属于原创文章,转载请注明本文地址!

作者QQ:1099718640

CSDN博客主页:http://blog.csdn.net/dyboy2017

Github开源项目:https://github.com/dyboy2017/spider


原创粉丝点击