pythonz
import requests

from lxml import etree

 

url = \'http://pic.netbian.com/4kqiche/\'

headers = {

    \'User-Agent\':\'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36\'

}

response = requests.get(url=url,headers=headers)

#手动设置响应数据的编码

# response.encoding = \'utf-8\'

page_text = response.text

 

tree = etree.HTML(page_text)

li_list = tree.xpath(\'//div[@class="slist"]/ul/li\')

for li in li_list:

    img_src = li.xpath(\'./a/img/@src\')[0]

    img_name = li.xpath(\'./a/b/text()\')[0]

    #通用性

    img_name = img_name.encode(\'iso-8859-1\').decode(\'gbk\')

    print(img_src,img_name) 

 

在我们爬取网页的时候,可能会遇到乱码的问题,解决这种的乱码的问题有一种通用的方法

 

分类:

技术点:

相关文章:

  • 2022-02-19
  • 2021-12-31
  • 2021-12-25
  • 2022-01-10
  • 2021-07-28
  • 2021-07-05
  • 2021-07-26
  • 2021-08-08
猜你喜欢
  • 2022-01-10
  • 2022-01-01
  • 2021-07-12
  • 2021-12-25
  • 2021-12-25
  • 2021-11-23
相关资源
相似解决方案