豆瓣电影python爬虫
时间: 2023-09-14 19:02:40 浏览: 153
豆瓣电影的Python爬虫可以用来获取豆瓣高分电影的相关信息,并制作成图表进行展示。以下是一个简单的豆瓣电影Python爬虫的代码示例:
```python
import requests
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
from fake_useragent import UserAgent
def url_parse():
url = "https://movie.douban.com/j/search_subjects?type=movie&tag=豆瓣高分&sort=rank&page_limit=20&page_start=0"
headers = {"User-Agent": UserAgent().random}
response = requests.get(url=url, headers=headers).json()
return response
def content_parse(res):
vedio_name = []
vedio_rate = []
content = res["subjects"]
for i in content:
name = i["title"]
rate = i["rate"]
vedio_name.append(name)
vedio_rate.append(float(rate))
return vedio_name, vedio_rate
def make_pic(name, rate):
fig = plt.figure(figsize=(15, 8), dpi=80)
font = FontProperties(fname=r"STZHONGS.TTF", size=12)
plt.barh(name[::-1], rate[::-1], color="red")
x_ = [i * 0.5 for i in range(1, 21)]
plt.xticks(x_, fontproperties=font)
plt.yticks(name, fontproperties=font)
plt.savefig("豆瓣.png")
plt.show()
response = url_parse()
vedio_name, vedio_rate = content_parse(response)
make_pic(vedio_name, vedio_rate)
```
这个爬虫的功能是通过发送HTTP请求获取豆瓣高分电影的数据,然后解析数据并提取电影的名称和评分,最后将这些数据制作成水平条形图进行展示。使用的库包括requests、matplotlib和fake_useragent。其中,requests用于发送HTTP请求,matplotlib用于制作图表,fake_useragent用于生成随机的User-Agent以模拟浏览器请求。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Python爬虫爬取豆瓣高分电影附源码(详细适合新手)](https://blog.csdn.net/gushuiwuqiu/article/details/117383666)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文