豆瓣电影排行简单数据爬取_pyhton

先安装一下requests和bs4库:

cmd下面:python -m pip install bs4 -i https://pypi.douban.com/simple

代码:

import requests
from bs4 import BeautifulSoup
url = 'https://movie.douban.com/top250?start='
header = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
start=0
for n in range(0,10):
html=requests.get(url+str(start), headers=header)
start+=25
soup = BeautifulSoup(html.text, 'html.parser')
for item in soup.find_all('div',class_="info"):
title=item.div.a.span.string #获取标题
yearline=item.find('div',class_="bd").p.contents[2].string #获取年份那一行
yearline=yearline.replace(' ','') #去掉这一行的空格
yearline=yearline.replace('\n','') #去掉这一行的回车换行
year=yearline[0:4] #只取年份前四个字符
score=item.find('span',class_="rating_num").contents[0].string
number=item.find('div',class_="star").contents[7].string
print(title,'\t',year,'\t',score,'\t',number)
posted @ 2020-08-06 16:03  LastOneBlackHole  阅读(75)  评论(0编辑  收藏  举报