标签:href 自己 ext code log sel esc 时间 rom
import requests from bs4 import BeautifulSoup net = ‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘ res = requests.get(net) res.encoding=‘utf-8‘ doc = BeautifulSoup(res.text,‘html.parser‘,from_encoding=‘utf-8‘) for news in doc.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 main = news.select(‘.news-list-description‘)[0].text source = news.select(‘.news-list-info‘)[0].contents[1].text print(‘Url:{}‘.format(news.select(‘a‘)[0][‘href‘])) print(‘标题:{}‘.format(news.select(‘.news-list-title‘)[0].text)) print(‘正文:{}‘.format(news.select(‘.news-list-description‘)[0].text)) print(‘时间:{}‘.format(news.select(‘.news-list-info‘)[0].contents[0].text)) print(‘来源:{}‘.format(news.select(‘.news-list-info‘)[0].contents[1].text))
用requests库和BeautifulSoup4库爬取新闻列表
标签:href 自己 ext code log sel esc 时间 rom
原文地址:http://www.cnblogs.com/knight-hui/p/7600954.html