找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2318|回复: 0

[求助] 关于Python图片爬虫的错误,求助

1

主题

1

帖子

1

积分

贫民

积分
1
CCJOE 发表于 2018-1-16 20:29:39 | 显示全部楼层 |阅读模式
代码如下:
import re
import urllib.request
def craw(url,page):
    htm11=urllib.request.urlopen(url).read()
    htm11=str(htm11)

    pat1='<div id="J_goodList".+?<div class="page clearfix">'
    result1=re.compile(pat1).findall(htm11)
    result1=result1[0]

    pat2='<img width="220" height="220" class="err-product" data-img="1" src="//(.+?\.jpg)">'
    imagelist=re.compile(pat2).findall(result1)

    x=1
    for imageurl in imagelist:
        imagename="D:/Urllib库/图片爬虫集/"+str(page)+str(x)+".jpg"
        imageurl="http://"+imageurl
        try:
            urllib.request.urlretrive(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,100):
     url="https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E6%89%8B%E6%9C%BA&cid2=653&cid3=655&page="+str(i)
     craw(url,i)


错误如下:
Traceback (most recent call last):
  File "<pyshell#71>", line 3, in <module>
    craw(url,i)
  File "<pyshell#65>", line 7, in craw
    imagelist=re.compile(plat2).findall(result1)
TypeError: expected string or bytes-like object

新手刚上手,不知怎么解决,请求大神帮助



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表