新练习
Posted lg916843
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了新练习相关的知识,希望对你有一定的参考价值。
import re
import requests
from bs4 import BeautifulSoup
from datetime import datetime
def getClickCount(newsUrl):
newId =re.search(‘\_(.*).html‘,newsUrl).group(1).split(‘/‘)[1]
clickUrl = "http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newId)
return(int(requests.get(clickUrl).text.split(‘.html‘)[-1].lstrip("(‘").rstrip("‘);")))
def getNewsDetail(newsUrl):
resd = requests.get(newsUrl)
resd.encoding = ‘utf-8‘
soupd = BeautifulSoup(resd.text,‘html.parser‘)
c = soupd.select(‘#content‘)[0].text
info = soupd.select(‘.show-info‘)[0].text
d = info.lstrip(‘发布时间;‘)[:19]
dt = datetime.strptime(d,‘%Y-%m-%d %H:%M:%S‘)
au = info[info.find(‘作者:‘):].split()[0].lstrip(‘作者:‘)
clickCount = getClickCount(newsUrl)
print(clickCount,newsUrl,dt,au)
def getNewsList(pageUrl):
res = requests.get(pageUrl)
res.encoding = ‘utf-8‘
soup = BeautifulSoup(res.text,‘html.parser‘)
for news in soup.select(‘li‘):
if len(news.select(‘.news-list-title‘))>0:
newsUrl = news.select(‘a‘)[0].attrs[‘href‘]
getNewsDetail(newsUrl)
break
pageUrl =‘http://news.gzcc.cn/html/xiaoyuanxinwen‘
getNewsList(pageUrl)
for i in (2,233):
getNewsList(pageUrl)
以上是关于新练习的主要内容,如果未能解决你的问题,请参考以下文章