豆瓣爬虫 python
时间: 2023-12-21 07:31:46 浏览: 83
以下是一个使用Python进行豆瓣爬虫的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 发送请求获取网页内容
url = 'https://movie.douban.com/top250'
response = requests.get(url)
html = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html, 'html.parser')
# 提取电影信息
movies = soup.find_all('div', class_='item')
for movie in movies:
title = movie.find('span', class_='title').text
rating = movie.find('span', class_='rating_num').text
print('电影名称:', title)
print('评分:', rating)
print('--')
```
这段代码使用了requests库发送请求获取豆瓣电影Top250的网页内容,然后使用BeautifulSoup库解析网页内容,提取出电影的名称和评分信息,并打印输出。
相关问题
python豆瓣爬虫
你想了解关于使用Python编写豆瓣爬虫的方法吗?以下是一个简单的爬取豆瓣电影Top250的示例代码:
```python
import requests
from bs4 import BeautifulSoup
def get_movie_info(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0;Win64) AppleWebkit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
movie_list = soup.find_all('div', class_='info')
for movie in movie_list:
title = movie.find('span', class_='title').text
rating = movie.find('span', class_='rating_num').text
print(f'Title: {title}, Rating: {rating}')
if __name__ == '__main__':
base_url = 'https://movie.douban.com/top250'
for i in range(10):
url = f'{base_url}?start={i * 25}'
get_movie_info(url)
```
这段代码使用了第三方库`requests`发送HTTP请求,以及`BeautifulSoup`解析HTML页面。设置了合适的请求头,避免被网站防爬机制拦截。然后通过解析页面的HTML结构,提取出电影的标题和评分信息,并进行打印输出。
请注意,爬取网站的信息时要遵守网站的规则和政策,确保合法合规。此外,频繁的爬取行为可能会对被爬取的网站造成压力,因此请适度使用爬虫。
豆瓣电影python爬虫
豆瓣电影的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 ]
阅读全文
相关推荐















