Python 新手爬虫的新手问题~~~求大神解惑!!! - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Yycreater
V2EX    Python

Python 新手爬虫的新手问题~~~求大神解惑!!!

  •  
  •   Yycreater
    Yycreater 2018-07-29 00:03:52 +08:00 2968 次点击
    这是一个创建于 2631 天前的主题,其中的信息可能已经有所发展或是发生改变。

    只是想爬几张图片,开始在处理,请求下载图片 src 的时候,返回的 403 响应,结果调着调着,就又报出了下面的 unicodeEncodeError,找了半天找到解决方法,基本主体代码都在这儿了(估计也没啥用)求大神解答!!!

    def handle_request(self, url): """构建请求对象:return""" headers = { 'User-Agent':' Mozilla / 5.0 ( Windows; U; Windows NT 6.1; en-us ) AppleWebKit / 534.50 ( KHTML,类似 Gecko )版本 / 5.1 Safari / 534.50', } return urllib.request.Request(url=url, headers=headers) def send_request(self, request): """发送请求获取内容""" return urllib.request.urlopen(request).read().decode('gbk') def down_picture(self, response): #根据内容形成 tree 对象 tree = etree.HTML(response) #根据数据形成对应 xpath pic_href = tree.xpath('//div[@class="main"]/dl/dd/a/img/@src') #图片链接 pic_text = tree.xpath('//div[@class="main"]/dl/dd/a[@target="_blank"]/text()') #图片文本 #pic_src://div[@class="main"]/dl/dd/a/img/@src #pic_text()://div[@class="main"]/dl/dd/a[@target="_blank"]/text() # try: for img in zip(pic_text,pic_href): # request = self.handle_request(img[1]) # 再次构建图片请求对象 # respOnse= self.send_request(request) # 发送对象返回响应 error403 dirname = './tupian';filename = img[0]+'.jpg' filepath = os.path.join(dirname, filename) if not os.path.exists(dirname): os.mkdir(dirname) # with open(filepath, 'wb') as fp: # fp.write(response.read()) urllib.request.urlretrieve(img[1],filepath) #图片下载 # except Exception as e: # print(e) 

    UnicodeEncodeError: 'latin-1' codec can't encode character '\uff08' in position 14: ordinal not in range(256) 有时间的话,顺便把 httperror403 错误也解了吧,新手,也没找到办法

    6 条回复    2018-07-30 10:10:38 +08:00
    matrix273
        1
    matrix273  
       2018-07-29 00:40:42 +08:00 via Android
    看一下 handle_request 返回的类型 是不是 b'',二进制类型啊,那样需要先 decode()吧,有一阵子没有捣鼓爬虫了。
    Sylv
        2
    Sylv  
       2018-07-29 01:09:49 +08:00
    403 一般就意味着网站检测出访问是非法的(例如爬虫),所以就拒绝访问了,你爬虫没有伪装好。

    UnicodeEncodeError 具体是哪行报的错?

    给个建议:
    人生苦短我用 requests。
    zhangpeter
        3
    zhangpeter  
       2018-07-29 06:47:50 +08:00
    用 Python3 的 requests 库吧,既方便,报错又少。
    ericls
        4
    ericls  
       2018-07-29 09:39:33 +08:00 via iPhone   1
    上面都推荐了 requests 我跟着推荐一下 requests-html
    API 不错
    rwecho
        5
    rwecho  
       2018-07-30 08:33:09 +08:00
    403 看看你的请求和浏览器的报文头有没有区别, 如果一样, 试着放慢采集的速度, 有可能太快被 ban 了

    UnicodeEncodeError 错误一般是转码问题, 你用的 terminal 是什么编码, 保存日志是什么编码, html 返回是什么编码.
    engineer9
        6
    engineer9  
       2018-07-30 10:10:38 +08:00
    .decode('gbk')去掉就行了,大佬哥
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5152 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 09:32 PVG 17:32 LAX 02:32 JFK 05:32
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86