欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

天津建设工程信息网爬取、乱码问题处理、http://www.tjconstruct.cn/Zbgg/Index/1?type=sgzb

程序员文章站 2022-04-02 11:11:57
1.直接上代码吧import requests,refrom requests.packages.urllib3.exceptions import InsecureRequestWarning# 禁用安全请求警告requests.packages.urllib3.disable_warnings(InsecureRequestWarning)# TODO http://www.tjconstruct.cn/Zbgg/Index/1?type=sgzburl = 'http://www.tj...

1.直接上代码吧

import requests,re
from requests.packages.urllib3.exceptions import InsecureRequestWarning
# 禁用安全请求警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

# TODO http://www.tjconstruct.cn/Zbgg/Index/1?type=sgzb

url = 'http://www.tjconstruct.cn/Zbgg/Index/1?type=sgzb'
headers = {
    'Host': 'www.tjconstruct.cn',
    'Connection': 'keep-alive',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cookie': 'ASP.NET_SessionId=pvcjnu2qlrigx2gvjwk5roa1',
}
r = requests.get(url=url,headers=headers).text

for i in re.findall('http://218.67.246.198:8089/shchxt/tonggao.doc(.*?)"', r, re.S):
    link = 'http://218.67.246.198:8089/shchxt/tonggao.doc'+i

    rr = requests.get(url=link, headers=headers)
    print(rr.encoding) # TODO 查看编码

    res_obj = requests.get(url=link,headers=headers).text
    print(res_obj.encode("iso-8859-1").decode('gbk'))
    break

本文地址:https://blog.csdn.net/shoujut/article/details/110876164

相关标签: 爬虫 python