今天试了下Python的爬虫,果然简单的一批,适合懵懂的小白。就先放代码吧。。。
from bs4 import BeautifulSoup
import requests
url = 'https://movie.douban.com/subject/35051512/comments?status=P'
r = requests.get(url=url,headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36'})
be = BeautifulSoup(r.text,features="lxml")
S = be.select('span[class=short]') #提取包含短评的部分
C = be.select('span[class=comment-info]') #提取包含评分的部分
for i,(s,c) in enumerate(zip(S,C)):
text = s.text.replace('\n','') #获取标签文本,删除换行
score = c.select('span[title]')[0].get('title') 提取评分,因为有两个title属性,评分是第一个。get('title')得到评分结果
print(i,text,score)
恩。直接运行代码