美文网首页
第四课 Python爬虫简单爬取新浪新闻列表

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

作者: DYBOY | 来源:发表于2017-09-06 14:55 被阅读206次

新闻列表页网页结构:

输出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

相关文章

网友评论

      本文标题:第四课 Python爬虫简单爬取新浪新闻列表

      本文链接:https://www.haomeiwen.com/subject/ggxvjxtx.html