经过了好久终于又开始续学Python,每天工作压力很大就算有时间感觉也没有多余精力学习。
这两天在网易云课堂买了一份Excel Power Pivot的教程给竞价岗位学,顺便看见了Python爬虫的教程就看了看,感觉之前的书是没有白看的。
学程序实操视频带来的教学比较适合我,除了能学到实际的教程外,可以看到高手们的编程思路,输写过程,很舒服。
这个是针对新浪的页面写的,原来BeautifulSoup能识别css和html标签,和之前玩的火车头及织梦采集相比,高明太多,怪不得是神器。
import requests
from bs4 import BeautifulSoup
def getNewsDetail(newsurl):
result = {}
res = requests.get(newsurl)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
result['title'] = soup.select('#artibodyTitle')[0].text
result['newssource'] = soup.select('.time-source span a')[0].text
timesource = soup.select('.time-source')[0].contents[0].strip()
result['dt'] = datetime.strptime(timesource, '%Y年%m月%d日%H:%M')
result['article'] = ' '.join([p.text.strip() for p in soup.select('#artibody p')[:-1]])
result['editor'] = soup.select('.article-editor')[0].text.strip('责任编辑:')
result['comments'] = getCommentCounts(newsurl)
return result