Python Requests库的一些基本用法

表单属性action

<form method="post" action="processing.php">
    First name:<input type="text" name="firstname"><br>
    Last name:<input type="text" name="lastname"><br>
    <input type="submit" value="submit">
</form>
import requests
parms = {'firstname':'firstname','lastname':'lastname'}
send = requests.post("php URL",data=parms)
print(send.text)
//未成功

上传图片

import requests
files = {'uploadFile':open('本地图片地址','rb')}
send = requests.post("php URL",files = files)
print(send.text)

处理登陆和cookie
用Requests库跟踪cookie

import requests
data = {'username':'uername','password':''}
send = requests.post('cookies welcome  php URL',data)
print(send.cookies.get_dict())

send = requests.get('cookies praofile php URL',cookies = send.cookies)
print(send.text)

session

import requests
session = requests.Session()

data = {'username':'','password':''}
send = session.post('cookies welcome php URL',data)

print(send.cookies.get_dict())

send = session.get('cookies profile php URL')
print(send.text)

网页的登陆

import requests
from requests.auth import AuthBase
from requests.auth import HTTPBasicAuth

auth = HTTPBasicAuth('账号','密码')
send = requests.post('登陆的php URL',auth=auth)
print(send.text)

提交文件和图像

import requests
files = {'uploadFile':open('图片的url','rb')}
r = requests.post("php url",files = files)
print(t.text)

模仿浏览器

import requests
from bs4 import BeautifulSoup

session = requests.Session()
headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit 537.36 (KHTML, like Gecko) Chrome","Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"}
url = ""
req = session.get(url, headers=headers)

bsObj = BeautifulSoup(req.text, "html.parser")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值