请帮我生成https://www.douban.com/的爬虫代码
时间: 2023-12-01 08:02:54 浏览: 28
好的,以下是Python爬虫代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.douban.com/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取所有电影名字和链接
movies = soup.find_all('div', class_='slide-page')
for movie in movies:
name = movie.find('div', class_='title').get_text().strip()
link = movie.find('a')['href']
print(name, link)
```
请注意,这只是一个简单的例子,实际的爬虫需要注意网站的反爬措施并且遵守爬虫规范。
相关问题
用Python 生成爬虫代码 网址https://movie.douban.com/ 爬取t250电影导演,演员保存到txt文件
好的,以下是使用 Python 爬取豆瓣电影 Top250 网站电影导演和演员并保存到本地 txt 文件的示例代码:
```python
import requests
from bs4 import BeautifulSoup
# 要爬取的网页地址
url = 'https://movie.douban.com/top250'
directors_actors = []
# 遍历每个页面
for i in range(10):
# 发起 GET 请求,获取网页内容
params = {'start': i * 25}
response = requests.get(url, params=params)
# 使用 BeautifulSoup 解析网页内容
soup = BeautifulSoup(response.text, 'html.parser')
# 找到网页中所有电影的链接
movie_links = soup.find_all('a', class_='title')
# 遍历每个电影的链接
for link in movie_links:
movie_url = link.get('href')
movie_response = requests.get(movie_url)
movie_soup = BeautifulSoup(movie_response.text, 'html.parser')
# 获取电影导演和演员信息
directors = movie_soup.find_all('a', rel='v:directedBy')
actors = movie_soup.find_all('a', rel='v:starring')
# 将导演和演员信息保存到列表中
for director in directors:
directors_actors.append(director.text)
for actor in actors:
directors_actors.append(actor.text)
# 将导演和演员信息保存到本地 txt 文件
with open('directors_actors.txt', 'w', encoding='utf-8') as f:
for name in directors_actors:
f.write(name + '\n')
```
这段代码使用 requests 库发起 GET 请求获取豆瓣电影 Top250 网站的前 250 部电影,然后使用 BeautifulSoup 库解析网页内容,找到所有电影的链接,遍历每个链接获取电影的导演和演员信息,并将这些信息保存到一个列表中。最后,将导演和演员信息写入到本地 txt 文件中。由于 Top250 网页是分页的,因此需要遍历每个页面,这里设置了遍历前 10 页。
用Python 生成爬虫代码 网址https://movie.douban.com/ 爬取电影图片保存到文件 爬取电影名,导演,评分,评价数,演员保存到txt文件并生成个电影的评价人数柱状图
好的,您想让我为您生成一个Python爬虫代码,用于从豆瓣电影网站(https://movie.douban.com/)中获取电影信息和图片,并将电影信息保存到txt文件中,并生成一个电影评价人数的柱状图,是吗?
首先,您需要安装以下Python库:requests、beautifulsoup4、matplotlib和pillow。请在终端中使用以下命令安装这些库:
```
pip install requests beautifulsoup4 matplotlib pillow
```
接下来,您可以使用以下代码来实现您的需求:
```python
import requests
from bs4 import BeautifulSoup
import os
from matplotlib import pyplot as plt
from PIL import Image
# 设置请求头,避免被网站封禁
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299'
}
# 获取电影列表页面
url = 'https://movie.douban.com/'
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
# 获取电影列表
movie_list = soup.find_all('div', class_='item')
# 创建文件夹用于保存电影海报
if not os.path.exists('movie_posters'):
os.mkdir('movie_posters')
# 创建文件用于保存电影信息
with open('movie_info.txt', 'w', encoding='utf-8') as f:
# 保存电影信息到文件
for movie in movie_list:
title = movie.find('span', class_='title').text
director = movie.find('div', class_='bd').p.text.split('\n')[1].strip().split(':')[1].strip()
rating = movie.find('span', class_='rating_num').text
ratings_count = movie.find('div', class_='star').find_all('span')[-1].text[:-3]
actors = movie.find('div', class_='bd').p.text.split('\n')[2].strip().split(':')[1].strip()
f.write(f'{title}\t{director}\t{rating}\t{ratings_count}\t{actors}\n')
# 保存电影海报到文件夹
img_url = movie.find('img')['src']
img_name = f"movie_posters/{title}.jpg"
img_response = requests.get(img_url, headers=headers)
with open(img_name, 'wb') as img_file:
img_file.write(img_response.content)
# 绘制电影评价人数的柱状图
with open('movie_info.txt', 'r', encoding='utf-8') as f:
data = f.readlines()
ratings_counts = [int(d.split('\t')[3]) for d in data]
titles = [d.split('\t')[0] for d in data]
plt.bar(titles, ratings_counts)
plt.xticks(rotation=90)
plt.xlabel('电影名')
plt.ylabel('评价人数')
plt.title('电影评价人数柱状图')
plt.savefig('ratings_counts.png')
# 打印完成信息
print('完成!')
```
这个代码将获取豆瓣电影网站上的电影信息和海报,并将电影信息保存到一个txt文件中,同时生成一个电影评价人数的柱状图。请注意,为了避免被网站封禁,我们使用了请求头来模拟浏览器请求。另外,由于豆瓣电影网站上的海报是通过JavaScript动态加载的,因此我们需要使用`requests`库来获取图像的二进制数据,并使用`Pillow`库来保存图像。
请让我知道是否有任何问题或者需要进一步的帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)