本例程使用urlib實(shí)現(xiàn)的,基于python2.7版本,采用beautifulsoup進(jìn)行網(wǎng)頁(yè)分析,沒有第三方庫(kù)的應(yīng)該安裝上之后才能運(yùn)行,我用的IDE是pycharm,閑話少說,直接上代碼!
# -*- coding: utf-8 -* import re import os import urllib import urllib2 from bs4 import BeautifulSoup def craw(url,page): html1=urllib2.urlopen(url).read() html1=str(html1) soup=BeautifulSoup(html1,'lxml') imagelist=soup.select('#J_goodsList > ul > li > div > div.p-img > a > img') namelist=soup.select('#J_goodsList > ul > li > div > div.p-name > a > em') #pricelist=soup.select('#plist > ul > li > div > div.p-price > strong') #print pricelist path = "E:/{}/".format(str(goods)) if not os.path.exists(path): os.mkdir(path) for (imageurl,name) in zip(imagelist,namelist): name=name.get_text() imagename=path + name +".jpg" imgurl="http:"+str(imageurl.get('data-lazy-img')) if imgurl == 'http:None': imgurl = "http:" + str(imageurl.get('src')) try: urllib.urlretrieve(imgurl,filename=imagename) except: continue ''' #J_goodsList > ul > li:nth-child(1) > div > div.p-img > a > img #plist > ul > li:nth-child(1) > div > div.p-name.p-name-type3 > a > em #plist > ul > li:nth-child(1) > div > div.p-price > strong:nth-child(1) > i ''' if __name__ == "__main__": goods=raw_input('please input the goos you want:') pages=input('please input the pages you want:') count =0.0 for i in range(1,int(pages+1),2): 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)) craw(url,i) count += 1 print 'work completed {:.2f}%'.format(count/int(pages)*100)
圖片的命名為商品的名稱,京東商品圖片地址的屬性很可能會(huì)有所變動(dòng),所以大家進(jìn)行編寫的時(shí)候應(yīng)該舉一反三,靈活運(yùn)用!?
這是我下載下來的手機(jī)類圖片文件的截圖:?
我本地的爬取的速度很快,不到一分鐘就能爬取100頁(yè)上千個(gè)商品的圖片!
以上這篇python利用urllib實(shí)現(xiàn)爬取京東網(wǎng)站商品圖片的爬蟲實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
