【无标题】

import string
import requests
from urllib import request, error
import time
from bs4 import BeautifulSoup
import os
import chardet
import array as arr
from lxml import etree

url = ‘http://ggzyjy.xzfwzx.putian.gov.cn/images/bg.jpg’

re = requests.get(url)

cookies = re.cookies

cookie = requests.utils.dict_from_cookiejar(cookies)

head = re.headers

re.encoding=requests.utils.get_encodings_from_content(re.text)

print(cookie,head, re.encoding)

def http_take(url1, url2= None, head=None, body=None):
‘’’
外部配置body
body{
‘username’:‘xxx’
‘password’:‘xxx’
}
‘’’
session = requests.session()
resp = session.post(url1, head, body)
‘’’
需要获取到
Connection: # 如果是keep-alive则表示是长连接
Content-Encoding # 判断内容编码格式,选取解码格式
Content-Type # 判断文件类型,如是文本文件需要进行解码
Cookie # 用户身份识别标识,用于http长连接
Status Code # 请求的响应状态,判断错误原因

'''
resp2 = session.get(url2, head)



return resp.status_code, resp2, session

resp = requests.get(url)

resp.encoding = chardet.detect(resp.content)[‘encoding’]

cookie = resp.cookies

print(cookie)

def get_Cookies():
headers = {
‘User-Agent’: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4515.159 Safari/537.36’
}
url = ‘http://ggzyjy.xzfwzx.putian.gov.cn/ptsq/005002/005002003/005002003001/subPageright.html’
session = requests.session()
s = session.post(url,headers=headers)
s.encoding=requests.utils.get_encodings_from_content(s.text)
cookie = session.cookies
a = cookie.get_dict()
return a, s
print(get_Cookies())

cookies = resp.cookies

en = resp.headers[‘Content-Encoding’]

# cookie = requests.utils.dict_from_cookiejar(cookies)

encode = resp.encoding

print(en)

def http_take(url, head=None, body=None):

if body != None:

try:

resp = requests.post(url, headers=head, body=body)

print(“正在进行post请求”)

except Exception as e:

print(“post请求出现了异常:{0}”.format(e))

else:

try:

resp = requests.get(url, headers=head)

print(“正在进行get请求”)

resp.raise_for_status() # 若状态码不是200,抛出HTTPError异常

resp.encoding = resp.apparent_encoding # 保证页面编码正确

resp.close()

except Exception as e:

print(“get请求出现了异常:{0}”.format(e))

except error.HTTPError as e:

print(e.code, ‘\n’, e.reason, ‘\n’, e.headers)

# 若出现HTTPError尝试用e.headers替换下面的headers

except error.URLError as e:

print(e.reason)

except requests.exceptions.ConnectTimeout:

print(‘超时!’)

except requests.exceptions.ConnectionError:

print(‘无效地址!’)

else:

print(‘HTMLand Request successfully’)

return resp.status_code, resp

if name == ‘main’:

http_take(url)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值