用Python代码爬取京东网页图片
时间: 2024-03-28 22:29:24 浏览: 65
你可以使用Python的requests和BeautifulSoup库来爬取京东网页图片。具体步骤如下:
1. 首先需要安装requests和BeautifulSoup库,可以使用以下命令进行安装:
```python
pip install requests
pip install beautifulsoup4
```
2. 然后使用requests库发送GET请求获取京东网页的HTML代码:
```python
import requests
url = 'https://item.jd.com/100008348542.html'
response = requests.get(url)
html = response.content.decode('utf-8')
```
3. 使用BeautifulSoup库解析HTML代码,获取图片链接:
```python
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
img_tags = soup.find_all('img')
img_links = []
for img in img_tags:
try:
img_links.append(img['src'])
except:
pass
```
4. 最后使用requests库下载图片:
```python
for link in img_links:
response = requests.get(link)
with open(link.split('/')[-1], 'wb') as f:
f.write(response.content)
```
上述代码将会将京东商品页面的所有图片下载到当前目录下。
阅读全文