标签:标题 内容 return parser turn tle pre [1] rom
1、获取单条新闻的#标题#链接#时间#来源#内容 #点击次数,并包装成一个函数。
2、获取一个新闻列表页的所有新闻的上述详情,并包装成一个函数。
3、获取所有新闻列表页的网址,调用上述函数。
4、完成所有校园新闻的爬取工作。
import requests import re from bs4 import BeautifulSoup from datetime import datetime url=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘ res=requests.get(url) res.encoding=‘utf-8‘ soup=BeautifulSoup(res.text,‘html.parser‘) def getclicks(current_urls): id = re.search("_(.*).html",current_urls).groups()[0].split(‘/‘)[1] click =int(requests.get(‘http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80‘.format(id)).text.split(‘.‘)[-1].lstrip("html(‘").rstrip("‘);")) return click #每一页文章明细 def getfirstpages(url24): res=requests.get(url24) 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 time=news.select(‘.news-list-info‘)[0].contents[0].text#时间 url1=news.select(‘a‘)[0][‘href‘]#url bumen=news.select(‘.news-list-info‘)[0].contents[1].text#院系 description=news.select(‘.news-list-description‘)[0].text#明细(文章) resd=requests.get(url1) resd.encoding=‘utf-8‘ soupd=BeautifulSoup(resd.text,‘html.parser‘) detail = soupd.select(‘.show-content‘)[0].text #内容 count = getclicks(url1)#调用函数 #re.match(‘http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1001/(.*).html‘,url).groups() print(title,count) #print(detail) #break n =int(soup.select(‘.a1‘)[0].text.rstrip(‘条‘)) number = n // 10 + 1 for i in range(2,number+1): url01=‘http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html‘.format(i) getfirstpages(url01)
import requests from bs4 import BeautifulSoup from datetime import datetime url=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘ res=requests.get(url) 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 time=news.select(‘.news-list-info‘)[0].contents[0].text#时间 url1=news.select(‘a‘)[0][‘href‘]#url bumen=news.select(‘.news-list-info‘)[0].contents[1].text description=news.select(‘.news-list-description‘)[0].text resd=requests.get(url1) resd.encoding=‘utf-8‘ soupd=BeautifulSoup(resd.text,‘html.parser‘) detail = soupd.select(‘.show-content‘)[0].text print(detail) break
import requests from bs4 import BeautifulSoup from datetime import datetime url=‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘ res=requests.get(url) 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 time=news.select(‘.news-list-info‘)[0].contents[0].text#时间 url1=news.select(‘a‘)[0][‘href‘]#url bumen=news.select(‘.news-list-info‘)[0].contents[1].text description=news.select(‘.news-list-description‘)[0].text resd=requests.get(url1) resd.encoding=‘utf-8‘ soupd=BeautifulSoup(resd.text,‘html.parser‘) detail = soupd.select(‘.show-content‘)[0].text #内容 click = requests.get(‘http://oa.gzcc.cn/api.php?op=count&id=8249&modelid=80‘).text.split(‘.‘)[-1].lstrip("html(‘").rstrip("‘);") print(click) print(detail) break
标签:标题 内容 return parser turn tle pre [1] rom
原文地址:http://www.cnblogs.com/lfsm/p/7650056.html