python爬取豆瓣明星图片
时间: 2023-11-04 12:05:19 浏览: 303
要使用Python爬取豆瓣明星图片,可以通过以下步骤实现:
1. 导入所需的库和模块,例如requests、BeautifulSoup等。
2. 使用requests库发送HTTP请求,获取豆瓣明星的主页HTML内容。
3. 使用BeautifulSoup库解析HTML内容,找到所有包含明星图片的a标签对象。
4. 遍历a标签对象列表,提取每个a标签中的图片链接。
5. 将图片链接保存到一个列表中。
6. 遍历图片链接列表,使用requests库下载每张图片并保存到本地。
以下是一个简单的代码示例,展示了如何使用Python爬取豆瓣明星图片:
```python
import requests
from bs4 import BeautifulSoup
# 发送HTTP请求,获取豆瓣明星的主页HTML内容
url = "https://movie.douban.com/celebrity/1011562/photos/"
response = requests.get(url)
html_content = response.text
# 解析HTML内容,找到所有包含明星图片的a标签对象
soup = BeautifulSoup(html_content, "html.parser")
a_list = soup.find_all("a", class_="cover")
# 提取每个a标签中的图片链接
picture_list = []
for a in a_list:
img_src = a.find("img")["src"]
picture_list.append(img_src)
# 下载图片并保存到本地
for picture_url in picture_list:
response = requests.get(picture_url)
with open("picture.jpg", "wb") as file:
file.write(response.content)
print("豆瓣明星图片已下载并保存到本地!")
```
阅读全文
相关推荐










