一 http请求方式连接网站数据
statusCode.py
import requests#在路径中直接添加get方式获取路径和状态码
url='https://www.baidu.com/get'
response=requests.get(url)
print(response)
print(response.status_code)
#无参数的get获取状态码方式
url='http://www.jd.com'
r=requests.get(url=url)
print(r.status_code)
print(r.url)
#有参数的get获取状态码方式
url='https://login.taobao.com/member/login.jhtml'
#字典格式
payload={
'spm':'a21bo.2017.754894437.1.5af911d9IUfLcO',
'f':'top',
'redirectURL':'https%3A%2F%2Fwww.taobao.com%2F'
}
#请求方式get路径和参数
r=requests.get(url=url,params=payload)
print(r)
print(r.url)
print(r.status_code)
print(r.content)
print(r.text)
result=r.content
if str(result).find('succ'):
print('admin:admin'+'succeeful')
#请求的post方式 带参数的请求
url='https://i.taobao.com/my_taobao.htm'
params={
"spm":"a21bo.2017.754894437.3.5af911d9j6H5ku",
"ad_id":"",
"cm_id":"",
"pm_id":"1501036000a02c5c3739"
}
r=requests.post(url=url,params=params)
print(r)
print(r.request)
print(r.status_code)
print(r.url)
print(r.text)
print(type(r.text))
if r.text.find('succ'):
print("successful")
url='http://www.baidu.com'
r=requests.get(url)
#得到网站请求头
r1=r.request.headers
print(r1)
#定义user-agent的值 可以改变固定值
url="http://www.jd.com"
headers={
"User-Agent":"my-sql"
}
r=requests.get(url=url,headers=headers)
print(r.request.headers)
控制台运行结果如下:
<class 'str'>
successful
{'User-Agent': 'python-requests/2.23.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
{'User-Agent': 'my-sql', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
二 ** 不同的http请求的不同结果
url='https://www.tmall.com/'
r=requests.get(url,params={
"ali_trackid":"2:mm_26632258_3504122_55934697:1600051047_178_2071692865",
"clk1":"4e5c8f90d4dfbc090f056171fea55794",
"upsid":"4e5c8f90d4dfbc090f056171fea55794",
"bxsign":"tbk16000510475478276d5e30e98700bd0143d2d468075d8"
})
print(r.status_code)
print(r.headers)
print(r.request.headers)
print(r.encoding)
print(r.url)
#此时cookies是空目录
print(r.cookies)
#当https的时候cookies为有数据状态 ]>
url="https://www.baidu.com"
r=requests.get(url)
print(r.cookies)
控制台输出结果
200
{'Server': 'Tengine', 'Content-Type': 'text/html; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding, Accept-Encoding, Origin, Ali-Detector-Type, X-Host, Accept-Encoding', 'Date': 'Mon, 14 Sep 2020 02:57:39 GMT', 'x-server-id': '28c3d6b2523ca52cb704b8b5dcd97677d231532c71c47d1b0f87559eae61f07c8bb00e660f25c2b1', 'realpath': 'page/portal/act/fp', 'Cache-Control': 'max-age=0, s-maxage=116', 'ETag': 'W/"36e89-v0m9Q3I/yOxHAam8+4z7Cz3ZtiI"', 'x-readtime': '85', 'x-via': 'cn2460.l1, bcache8.cn2460, l2cn859.l2, cache17.l2cn859, wormholesource011088033031.center.na61', 'EagleEye-TraceId': '7beb211c16000522590061050e', 'Strict-Transport-Security': 'max-age=0, max-age=31536000', 'Timing-Allow-Origin': '*, *', 'Ali-Swift-Global-Savetime': '1600052259', 'Via': 'cache17.l2cn859[176,200-0,C], cache35.l2cn859[15,0], bcache2.cn2583[0,200-0,H], bcache4.cn2583[1,0]', 'Age': '99', 'X-Cache': 'HIT TCP_MEM_HIT dirn:-2:-2', 'X-Swift-SaveTime': 'Mon, 14 Sep 2020 02:57:39 GMT', 'X-Swift-CacheTime': '116', 'EagleId': '3cdd499816000523589496905e', 'Content-Encoding': 'gzip'}
{'User-Agent': 'python-requests/2.23.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
utf-8
https://www.tmall.com/?ali_trackid=2%3Amm_26632258_3504122_55934697%3A1600051047_178_2071692865&clk1=4e5c8f90d4dfbc090f056171fea55794&upsid=4e5c8f90d4dfbc090f056171fea55794&bxsign=tbk16000510475478276d5e30e98700bd0143d2d468075d8
<RequestsCookieJar[]>
<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
网友评论