python网页请求_python发送网络请求

1、使用urllib模块(使用不方便,建议使用第二种)

get请求: res = urlopen(url)

from urllib.request importurlopen

url= 'http://www.nnzhp.cn'

print(urlopen(url))#返回http.client.HTTPResponse object at 0x00000235BA25A160

print(urlopen(url).read().decode())#返回get到的页面的源代码#decode是将base类型转为encoding 指定的编码格式解码字符串,不指定则转为默认编码,如utf-8

post请求:

from urllib.request importurlopenfrom urllib.parse importurlencode

url='http://127.0.0.1:8999/api/login'data= {'username':'testuser1','passwd':'111111'}

s=urlencode(data)

res= urlopen(url,s.encode()) #post请求

print(res.read().decode())#最后print返回数据未请求返回数据

{"code": 2,"msg": "用户不存在"}

注意:带参数的时候是一个post请求,若http://127.0.0.1:8999/api/login接口的方法限定了methods为get,则会报错urllib.error.HTTPError: HTTP Error 405: METHOD NOT ALLOWED

res.read().decode()  #read()返回的值是base类型,需要通过decode转成utf-8等;如果返回类型是json,不能使用json()方法将其转成字典

2、使用requests模块

先安装requests模块:pip install requests

然后导入该模块 import requests

import requests后运行脚本文件,报错如下openssl相关的信息,则重新安装pyOpenSSL即可。

1 AttributeError: cffi library '_openssl' has no function, constant or global variable named 'Cryptography_HAS_NETBSD_D1_METH'

get请求:res = requests.get(url,data=data,cookies=cookie,headers=header,verify=False,files=file)

data可传可不传,data是字典格式。

如果url是https的话,加上verify=False。如果url是http的话,可不加。

importrequests

url='http://127.0.0.1:8999/api/login'data= {'username':'testuser1','passwd':'111111'}

r= requests.get(url,params=data) #发get请求

print(r)#返回

print(r.json())#将返回的json串转为字典

print(r.text)#返回get到的页面的返回数据

输出结果:

{'code': 2, 'msg': '用户不存在'}

{"code": 2,"msg": "用户不存在"}

post请求

res = requests.post(url,data=data,cookies=cookie,headers=header,verify=False,files=file)

data是字典格式

res = requests.post(url,json=data,cookies=cookie,headers=header,verify=False,files=file)

data是json格式

Cookie、header里面只能放String 类型的键值对。不能是json格式

file是二进制文件

如果url是https的话,加上verify=False。如果url是http的话,可不加。

importrequests## --------------------------------------------------------------------## 发送post请求--带参数(字典格式)## --------------------------------------------------------------------

url1 = 'http://127.0.0.1/api/user/login'data= {'username':'testuser1','passwd':'111111'}

res= requests.post(url1,data=data)#data可以直接传字典

print('2.1=====',res)print('2.2=====',res.json())#返回结果不需要decode,也可以直接通过json()转成字典

print('2.3=====',res.text)

发送post请求--带参数

importrequests## --------------------------------------------------------------------## 发送post请求--带参数(json格式)## --------------------------------------------------------------------

url3= 'http://127.0.0.1/api/user/add_stu'data= {"name":"request_name","grade":"3","sex":"男","age":28,"addr":"河南省"}

res= requests.post(url3,json=data)#注意,这里的data是字典格式!之所以json=data,是因为请求报文要求入参是json格式,requests模块会自己将data字典转成json格式再发送请求

print('3.1=====',res)print('3.2=====',res.json())print('3.3=====',res.text)

发送post请求--带cookie(仅字典格式)

importrequests#获取cookie值

url= 'http://127.0.0.1/api/user/login'data= {'username':'testuser1','passwd':'111111'}

res= requests.post(url,data=data)

result=res.json()print('4.0=====',result)

sign= result.get('login_info').get('sign')## --------------------------------------------------------------------## 发送cookie## --------------------------------------------------------------------

url4= 'http://127.0.0.1/api/user/gold_add'data= {'stu_id':15,'gold':1000}

cookie={'testuser1':sign}

res= requests.post(url4,data=data ,cookies=cookie)#data是字典

print('4.1=====',res)print('4.2=====',res.json())print('4.2=====',res.text)

发送post请求--带header(仅字典格式)

cookie如果有很多键值对的话,可以包装在header里发出,header={'Cookie':'a=v,b=v........'}

importrequests## --------------------------------------------------------------------## 发送header## --------------------------------------------------------------------

url5 = 'http://127.0.0.1/api/user/all_stu'header= {'Referer':'http://api.nnzhp.cn/'}

res= requests.get(url5,headers=header)#传header

print('5.1=====',res)print('5.2=====',res.json())print('5.3=====',res.text)

res= requests.get(url5)#不传header

print('5.4=====',res.json())

requests.get(url,headers={'cookie':'pt2gguin=o0511402865; RK=JQZpwBp1by;'

'ptcz=6c30e26a9ed6be93d3de9e4c4aca3e55650cf99fcffa64729bd1d58a5fb209d9;'

'pgv_pvi=779236352; pgv_pvid=6970909788; qb_qua=; qb_guid=818de686e29d412fa4ee9e99905ea166;'

'Q-H5-GUID=818de686e29d412fa4ee9e99905ea166; NetType=; pgv_si=s4499960832;'

'FTN5K=0138ca95; pgv_info=ssid=s4269564370; luin=o0511402865; uin=o0511402865;'

'lskey=00010000efc2701412d3429029ac9366e4ba98f0e978e0ae4a9c684101a7b22df0695f534bc242c8d4ff386d;'

'skey=@0sHtvhTsD; ptisp=cnc; p_uin=o0511402865; pt4_token=wGU2YAaM0uu7LbcNjuDcLN-TPrEy7AZw4gcL5TEcKxw_;'

'p_skey=1zg7yvF5wr6l43mfr-BvNHxuVDtybKpR5RbCrgC8weQ_'})#cookie传入,直接拷贝进去

requests.get(url,cookies={'pt2ggui':'o0511402865','RK':'JQZpwBp1by'})#写cookie的话,要一个个按照字典的方式写

发送post请求--带file(仅二进制格式)

r =requests.post(url,

data={'session_id':'6ab8785039dcf50fb11c53acc1db7648'},

files={'file_name':open('account.xls','rb') }

)

importrequests## --------------------------------------------------------------------## 上传文件## --------------------------------------------------------------------

url7 = 'http://127.0.0.1/api/file/file_upload'data= {'file':open('魔鬼中的天使.mp3','rb')}

res= requests.post(url7,files=data)#如果是https的话,要加上verfiy=False参数

print('3.1=====',res.json())

获取响应结果

res  #返回如

res.status_code  #返回状态码,如200

1 import requests

2 ## --------------------------------------------------------------------

3 ## 返回状态码

4 ## --------------------------------------------------------------------

5 url = 'http://www.nnzhp.cn'

6 res = requests.get(url)#发送get请求

7 print('1.1======',res.status_code)#获取状态码

1 1.1====== 200

res.json()  #返回字典。不需要手动decode()转码。如果res结果是json格式,可以使用json()将json串转成字典格式。如果res结果不是json的话,不能使用json()

res.text#返回字符串,响应的源码。不能用于下载文件。

res.content  #返回二进制。主要用于流媒体文件、图片文件的下载。

下载mp3

importrequests## --------------------------------------------------------------------## 返回二进制--流媒体## --------------------------------------------------------------------

url6 = 'http://qiniuuwmp3.changba.com/1084511584.mp3'res=requests.get(url6)

with open('魔鬼中的天使.mp3','wb') as fw:#把二进制文件下载下来,写入到'魔鬼中的天使.mp3'

fw.write(res.content)#res.content返回结果是二进制的,如x03\xe3\xa3\xf2......

下载图片

importrequests## --------------------------------------------------------------------## 返回二进制--图片## --------------------------------------------------------------------

url7 = 'https://aliimg.changba.com/cache/photo/855e5493-f018-44db-8892-c8660649327b_640_640.jpg'res= requests.get(url7,verify=False)#如果是https的话,要加上verfiy=False参数

print('3.3=====',res.content)#返回结果是二进制的

with open('photo.jpg','wb') as fw:#把二进制文件下载下来,写入到'photo.jpg'

fw.write(res.content)

res.headers  #返回响应的所有headers

## --------------------------------------------------------------------## 返回headers## --------------------------------------------------------------------

url = 'http://www.nnzhp.cn'res= requests.get(url)#发送get请求

print('1.1======',res.headers)#获取所有cookies

res.cookies  #返回响应的所有cookies

importrequests## --------------------------------------------------------------------## 返回cookie## --------------------------------------------------------------------

url = 'http://www.nnzhp.cn'res= requests.get(url)#发送get请求

print('1.1======',res.cookies)#获取所有cookies

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值