标签:coding parse new sele 操作 port pre 爬取 soup
import requests from bs4 import BeautifulSoup from datetime import datetime news=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘ res=requests.get(news) res.encoding=‘utf-8‘ soup=BeautifulSoup(res.text,‘html.parser‘) def getdetail(url): resd=requests.get(url) resd.encoding=‘utf-8‘ soup=BeautifulSoup(resd.text,‘html.parser‘) return(soup.select(‘.show-content‘)[0].text) 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) #时间 dt=datetime.strptime(time,‘%Y-%m-%d‘) where=(news.select(‘.news-list-info‘)[0].contents[1].text) #来源 resd=requests.get(url) resd.encoding=‘utf-8‘ soup=BeautifulSoup(resd.text,‘html.parser‘) detail=getdetail(url) #详情 print(dt,title,url,where,detail)
import requests from bs4 import BeautifulSoup news=‘http://www.gdrsks.gov.cn/‘ res=requests.get(news) res.encoding=‘utf-8‘ soup=BeautifulSoup(res.text,‘html.parser‘) for news in soup.select (‘li‘): if len(news.select(‘.node‘))>0: t=news.select(‘.node‘)[0].text k=news.select(‘.title‘)[0].text url=news.select(‘a‘)[0][‘href‘] #链接 print(t,k,url)
用requests库和BeautifulSoup4库爬取新闻列表
标签:coding parse new sele 操作 port pre 爬取 soup
原文地址:http://www.cnblogs.com/lcm1995/p/7607061.html