码迷,mamicode.com
首页 > 其他好文 > 详细

用requests库和BeautifulSoup4库爬取新闻列表

时间:2017-09-28 16:53:43      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:res   img   contents   bs4   pre   encoding   分享   style   html   

import requests
from bs4 import BeautifulSoup

gzccurl = ‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘
res = requests.get(gzccurl)
res.encoding=‘utf-8‘

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

for news in soup.select(‘li‘):
    if len(news.select(‘.news-list-title‘))>0:
        title = news.select(‘.news-list-title‘)[0].text#标题
        url = news.select(‘a‘)[0][‘href‘]#链接
        time = news.select(‘.news-list-info‘)[0].contents[0].text
        source = news.select(‘.news-list-info‘)[0].contents[1].text
        
        #详情
        #resd = requests.get(url)
        #res.encoding=‘utf-8‘
        #soupd = BeautifulSoup(res.text,‘html.parser‘)
        #detail = soupd.select(‘.show-content‘)

        print(time,‘\n‘,title,‘\n‘,url,‘\n‘,source,‘\n‘

技术分享

 

用requests库和BeautifulSoup4库爬取新闻列表

标签:res   img   contents   bs4   pre   encoding   分享   style   html   

原文地址:http://www.cnblogs.com/diaowen/p/7607075.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!