|
import re
import urllib.request
def craw(url,page):
html1=urllib.request.urlopen(url).read()
html1=str(html1)
pat1='<div id="J_feature".+?<div class="page clearfix">'
result1=re.compile(pat1).findall(html1)
result1=result1[0]
pat2='<img width="200" height="220" class="err-product" data-img="1" source-data-lazy-img="//(.+?\.jpg)">'
imagelist=re.compile(pat2).findall(result1)
x=1
for imageurl in imagelist:
imagename="E:/爬取的图片/"+str(page)+str(x)+".jpg"
imageurl="http://"+imageurl
try:
urllib.request.urlretrieve(imageurl,filename=imagename)
except urllib.error.URLError as e:
if hasattr(e,"code"):
x+=1
if hasattr(e,"reason"):
x+=1
x+=1
for i in range(1,5):
url="https://search.jd.com/Search?keyword=ipad&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&bs=1&wq=ipad&ev=exbrand_Apple%5E&page=str(2*i-1)&s=str((i-1)*60+1)&click=0"
craw(url,i)
请问一下,程序好像没问题,就是不知道为什么不出图片,小白求大神指教
|
|