1、爬取豆瓣Top250电影信息以文本形式保存 数据:豆瓣电影
2、代码
import requests
from bs4 import BeautifulSoup
def get_movie():
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X metaSr 1.0'} # 创建头部信息
movie_list=[['序号','标题','推荐语','演员','链接']]
for i in range(0,10):
url = 'https://movie.douban.com/top250?start='+str(i*25)
response=requests.get(url,headers=headers)
soup=BeautifulSoup(response.text,"html.parser")
ol = soup.find('ol', class_='grid_view')
li_list=ol.find_all('li')
for each in li_list:
no=each.find('em').text
title = each.find('span', class_="title").text
inq = each.find('span', class_='inq')
star = each.find('p').text
href=each.find('a')['href']
movie_list.append([no,title,inq.text if inq!=None else '',star,href])
return movie_list
movie=[]
movie=get_movie()
with open("Top_movie_250.txt","a+",encoding="utf-8") as f:
for i in movie:
f.write(str(i))
f.write("n")
f.close()
movie.py


