python利用urllib实现的爬取京东网站商品图片的爬虫

本文介绍了一个使用Python 2.7及BeautifulSoup实现的京东商品图片爬虫程序。该程序可以抓取指定商品的图片,并按商品名保存到本地文件夹中。文中详细展示了爬虫代码,包括网页请求、解析及图片下载等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本例程使用urlib实现的,基于python2.7版本,采用beautifulsoup进行网页分析,没有第三方库的应该安装上之后才能运行,我用的IDE是pycharm,闲话少说,直接上代码!

 1 # -*- coding: utf-8 -*
 2 import re
 3 import os
 4 import urllib
 5 import urllib2
 6 from bs4 import BeautifulSoup
 7 def craw(url,page):
 8     html1=urllib2.urlopen(url).read()
 9     html1=str(html1)
10     soup=BeautifulSoup(html1,'lxml')
11     imagelist=soup.select('#J_goodsList > ul > li > div > div.p-img > a > img')
12     namelist=soup.select('#J_goodsList > ul > li > div > div.p-name > a > em')
13     #pricelist=soup.select('#plist > ul > li > div > div.p-price > strong')
14     #print pricelist
15     path = "E:/{}/".format(str(goods))
16     if not os.path.exists(path):
17         os.mkdir(path)
18     for (imageurl,name) in zip(imagelist,namelist):
19         name=name.get_text()
20         imagename=path + name  +".jpg"
21         imgurl="http:"+str(imageurl.get('data-lazy-img'))
22         if imgurl == 'http:None':
23             imgurl = "http:" + str(imageurl.get('src'))
24         try:
25             urllib.urlretrieve(imgurl,filename=imagename)
26         except:
27             continue
28 
29 '''
30 #J_goodsList > ul > li:nth-child(1) > div > div.p-img > a > img
31 #plist > ul > li:nth-child(1) > div > div.p-name.p-name-type3 > a > em
32 #plist > ul > li:nth-child(1) > div > div.p-price > strong:nth-child(1) > i
33 '''
34 
35 if __name__ == "__main__":
36     goods=raw_input('please input the goos you want:')
37     pages=input('please input the pages you want:')
38     count =0.0
39     for i in range(1,int(pages+1),2):
40         url="https://search.jd.com/Search?keyword={}&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&suggest=1.def.0.T06&wq=diann&page={}".format(str(goods),str(i))
41         craw(url,i)
42         count += 1
43         print 'work completed {:.2f}%'.format(count/int(pages)*100)

 

图片的命名为商品的名称,京东商品图片地址的属性很可能会有所变动,所以大家进行编写的时候应该举一反三,灵活运用! 
这是我下载下来的手机类图片文件的截图: 
这里写图片描述
我本地的爬取的速度很快,不到一分钟就能爬取100页上千个商品的图片!

转载于:https://www.cnblogs.com/kfpa/p/7418843.html

Python中,使用`urllib`库爬取京东商品信息通常涉及到HTTP请求和解析HTML内容。`urllib`库本身主要用于发送HTTP请求,而解析HTML可以借助更强大的第三方库如`BeautifulSoup`或`lxml`。以下是简单的步骤: 1. **安装所需库**: 首先,确保已安装`requests`库以便处理HTTP请求,如果尚未安装,可以运行`pip install requests`。 2. **发送GET请求**: 使用`requests.get()`函数向京东商品详情页URL发送请求,获取HTML源码。例如: ```python import requests url = 'https://item.jd.com/<商品ID>.html' response = requests.get(url) ``` 3. **检查响应状态**: 确保请求成功,检查`response.status_code`,通常是200表示成功。 4. **解析HTML**: 使用`BeautifulSoup`或`lxml`库解析HTML内容: ```python from bs4 import BeautifulSoup soup = BeautifulSoup(response.text, 'lxml') ``` 5. **定位信息元素**: 根据网页结构查找商品标题、价格、图片等信息所在的HTML标签,这需要熟悉京东商品页面的HTML结构。 6. **提取数据**: 对找到的元素应用`.text`或`.get('src')`等方法获取你需要的数据。 7. **保存或输出数据**: 将提取到的数据存储到文件或直接打印出来。 ```python title = soup.find('div', class_='J_MoreTitle').find('h1').text price = soup.find('span', class_='p-price').text image_url = soup.find('img', alt='商品图片')['src'] print(f"商品标题: {title}") print(f"商品价格: {price}") print(f"商品图片: {image_url}") ``` 注意:实际操作时,京东可能有反爬虫机制,频繁抓取可能会触发封IP,因此在编写爬虫时要遵守网站的robots.txt协议,并尽量模拟真实用户访问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值