python 爬取豆瓣top100电影页面

该博客介绍了如何使用Python爬虫爬取豆瓣Top100电影页面,首先保存网页以避免频繁访问,然后利用BeautifulSoup解析HTML获取电影排名、名称、导演、主演、评分和海报图片,并将信息存储为TXT和图片文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python 爬取豆瓣top100电影页面

运行结果截图:
在这里插入图片描述
在这里插入图片描述

代码:
(原网站:https://movie.douban.com/top250)

1、将页面保存 避免多次访问
(登陆状态需页面cookie)

import requests

url="https://movie.douban.com/top250"
headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36 Edg/90.0.818.46','cookie':'bid=sO6ny0NVNqQ; _pk_id.100001.4cf6=77175839ae9b2c3b.1619686194.1.1619686194.1619686194.; _pk_ses.100001.4cf6=*; ap_v=0,6.0; __utma=30149280.35012747.1619686195.1619686195.1619686195.1; __utmb=30149280.0.10.1619686195; __utmc=30149280; __utmz=30149280.1619686195.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=223695111.351459569.1619686195.1619686195.1619686195.1; __utmb=223695111.0.10.1619686195; __utmc=223695111; __utmz=223695111.1619686195.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __yadk_uid=URY22xM5zXDRrprNmeoAzbBY6gxEoqR9; __gads=ID=06784ca21b6f77c2-22a7788cafc70011:T=1619686197:RT=1619686197:S=ALNI_MbW9cpCEeKsCd89PFHC4T-1d80nKg'}
r = requests.get(url,headers=headers)
r.encoding = r.apparent_encoding
html= r.text

#print(html)

with open("maoyan250_page1.html","w+",encoding="utf-8") as f:
    f.write(html)


print("爬取完毕!")

2、爬取第一页

import requests
from bs4 import BeautifulSoup

f=open("maoyan250_page1.html",encoding="utf-8")
html=f.read()
#print(html)

soup=BeautifulSoup(html,"lxml")

movies = soup.select('ol li')
for movie in movies:
    rank = movie.find(name = 'em').get_text()
    filmname = movie.find(name = "span",class_='title').get_text()
    star = movie.find_all("p")[0].text.strip()  #用strip去除两边空格。
    star_split = star.split("主演:")     #使用回车(\n),切割information为两段
    daoyan = star_split[0]     # 第一段为导演和演员的信息
    zhuyan = star_split[1].split("\n")[0]     # 第二段为年份、地点、类型的信息
    score = movie.find(class_="rating_num").get_text()
    pic = movie.find(name='img').get('src')
    path = filmname+".jpg"
    pic_r = requests.get(pic)
    with open(path,"wb")as f:
        f.write(pic_r.content)
    print(rank,filmname,daoyan,zhuyan,score)
        
    content=[rank,filmname,daoyan,zhuyan,score]   #将爬取内容存储为txt
    with open("douban.txt","a+",encoding="utf-8") as file:
        file.write(str(content))
        
print("第一页爬取完毕!文件写入完毕!")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值