• 5.2 个人作业2


    爬虫中遇到的问题

     关于爬取时,网页编码不是utf-8,而导致 lxml 输出中文时乱码的解决办法

    用requests.get拿到response,response.content是bytes的内容,所以可以直接传给 lxml, body = etree.HTML(response.content)就不会有乱码了

    而response.text是返回unicode编码的内容,需要进行编码(encode),所以就是 body = etree.HTML(response.text.encode(response.encoding))

    其实在python3中只要理解 str 和 bytes 的关系就够了。

    有一篇文章讲得很好:http://www.ituring.com.cn/article/1116

    ps:其实python3已经比python2少了很多很多编码转换的问题了。。。

    在爬取某个网站时,直接用lxml.etree对response.content进行分析拿到的数据,与保存到本地后再分析拿到数据不一致

    复制代码
     1 url = 'http://op.hanhande.com/mh/'
     2 HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:51.0) Gecko/20100101 Firefox/51.0 '}
     3 
     4 #直接分析
     5 response = requests.get(url, headers=HEADERS)
     6 body = etree.HTML(response.content)
     7 us = body.xpath('//div[@class="mh_list_li"]/ul/li/a/@href')
     8 ts = body.xpath('//div[@class="mh_list_li"]/ul/li/a/text()')
     9 print(len(us), len(ts))
    10 #保存后分析
    11 with codecs.open('body.html', 'w', encoding = response.encoding) as f:
    12     f.write(response.content.decode(response.encoding))
    13 with codecs.open('body.html', 'r') as f:
    14     body = f.read()
    15     body = etree.HTML(body)
    16     us = body.xpath('//div[@class="mh_list_li"]/ul/li/a/@href')
    17     ts = body.xpath('//div[@class="mh_list_li"]/ul/li/a/text()')
    18     print(len(us), len(ts))
    复制代码

    运行结果为:

    14 14
    582 582

    猜测可能是编码的问题,但是不知道如何确定。

  • 相关阅读:
    About Me
    洛谷 P2633 Count on a tree
    【题解】牛客编程巅峰赛S1赛季第1场
    洛谷 P4132 [BJOI2012]算不出的等式
    洛谷 P5970 [POI2016]Nim z utrudnieniem
    DP没入门就入土
    洛谷 P4042 [AHOI2014/JSOI2014]骑士游戏
    洛谷 P3592 [POI2015]MYJ
    51Nod 1683 最短路
    51Nod 1327 棋盘游戏
  • 原文地址:https://www.cnblogs.com/dty602511/p/14914499.html
Copyright © 2020-2023  润新知