python 爬取豆瓣top100电影页面

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("第一页爬取完毕!文件写入完毕!")
### 回答1: 如果你想爬取豆瓣TOP100电影,你可以使用Python的第三方库来帮助你爬取信息。 你可以使用Python的requests库来发送HTTP请求并获取网的内容,再使用Python的Beautiful Soup库来解析网并提取你想要的信息。 下面是一个简单的例子,它爬取豆瓣TOP100电影电影名和评分: ```python import requests from bs4 import BeautifulSoup # 发送HTTP请求并获取网内容 url = 'https://movie.douban.com/chart' response = requests.get(url) html = response.text # 使用Beautiful Soup解析网并提取信息 soup = BeautifulSoup(html, 'html.parser') movies = soup.find_all(class_='pl2') for movie in movies: title = movie.find('a')['title'] rating = movie.find(class_='rating_nums').text print(title, rating) ``` 在这个例子中,我们使用了requests库发送HTTP请求并获取了网的内容,然后使用Beautiful Soup解析网并提取了我们想要的信息。 希望这个例子对你有帮助! ### 回答2: 要使用Python爬取豆瓣TOP100电影,首先需要安装相关的爬虫库。比较常用的库有Requests和BeautifulSoup。 1. 首先,我们需要导入所需的库: ``` import requests from bs4 import BeautifulSoup ``` 2. 然后,我们可以使用Requests库向豆瓣TOP100电影的网发送请求,并获取其HTML代码: ``` url = 'https://movie.douban.com/top250' response = requests.get(url) html_content = response.text ``` 3. 接下来,我们可以使用BeautifulSoup解析HTML代码,从中提取出我们需要的信息,如电影名称、评分等: ``` soup = BeautifulSoup(html_content, 'html.parser') movie_items = soup.find_all('div', class_='hd') for item in movie_items: movie_name = item.a.span.text.strip() print(movie_name) ``` 4. 上述代码中,我们使用了find_all方法来获取所有class属性为'hd'的div标签,然后通过item.a.span.text来提取出电影名称。你可以根据自己的需求提取其他信息,并将其存储到一个列表或文件中。 总的来说,使用Python爬取豆瓣TOP100电影需要的关键步骤就是发送请求、解析HTML代码以及提取数据。以上代码只是一个简单示例,你可以根据自己的需求进行更加详细和复杂的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值