Requests的基本使用
Request官网文档:https://docs.python-requests.org/zh_CN/latest/
import requests
url = 'https://www.baidu.com'
res = requests.get(url=url)
print(type(res))
res.encoding = 'utf-8'
print(res.text)
print(res.url)
print(res.content)
print(res.status_code)
print(res.headers)
Requests_get请求
import requests
url = 'https://www.baidu.com/s?'
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'
}
data = {
'wd':'北京'
}
res = requests.get(url=url,params=data,headers=head)
content = res.text
print(content)
Requests_post请求
import requests
import json
url = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'
head ={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}
data = {
'query': '眼镜'
}
res = requests.post(url=url,data=data,headers=head)
content = res.text
obj = json.loads(content)
print(obj)
Requests 代理
import requests
url = 'https://www.baidu.com/s?'
head ={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}
data = {
'wd': 'ip'
}
proxy = {
'https':'212.129.250.55:16816'
}
res = requests.get(url=url,params=data,headers=head,proxies=proxy)
content = res.text
with open('daili.html','w',encoding='utf-8') as fp:
fp.write(content)
Requests_cookie登录
通过登陆 然后进入到主页面
'''
__VIEWSTATE: 4xNKK4WTIZERi2yXW2RFFwsK6D+/AYZvM+D2YbmJnRXH+xUcVJQFsj2JrDKFkJ7yCMQaxmElcJjge7uP93ho3ms/QilejHP0X+uUSK3w3Clx5g5hTpFT7HqJZtE=
__VIEWSTATEGENERATOR: C93BE1AE
from: http://so.gushiwen.cn/user/collect.aspx
email: 123123123@qq.com
pwd: nidaye
code: MOIM
denglu: 登录
'''
import requests
from bs4 import BeautifulSoup
import urllib.request
url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
head = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36'
}
res = requests.get(url=url,headers=head)
con = res.text
soup = BeautifulSoup(con,'lxml')
VIEWSTATE = soup.select('#__VIEWSTATE')[0].attrs.get('value')
VIEWSTATEGENERATOR = soup.select('#__VIEWSTATEGENERATOR')[0].attrs.get('value')
code = soup.select('#imgCode')[0].attrs.get('src')
code_url = 'https://so.gushiwen.cn' + code
session = requests.session()
res_code = session.get(code_url)
content_code = res_code.content
with open('code.jpg','wb') as fp:
fp.write(content_code)
code_name = input('请输入验证码:')
url_post = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
data = {
'__VIEWSTATE': VIEWSTATE,
'__VIEWSTATEGENERATOR': VIEWSTATEGENERATOR,
'from': 'http://so.gushiwen.cn/user/collect.aspx',
'email': '595165358@qq.com',
'pwd': 'action',
'code': code_name,
'denglu': '登录',
}
resquest = session.post(url=url_post,headers=head,data=data)
content = resquest.text
with open('gushiwen.html','w',encoding='utf-8') as f:
f.write(content)
Cookie登录附件
通过超级鹰,识别图片验证码。这个是收费的,不同类型的验证码价格不一样。
import requests
from hashlib import md5
class Chaojiying_Client(object):
def __init__(self, username, password, soft_id):
self.username = username
password = password.encode('utf8')
self.password = md5(password).hexdigest()
self.soft_id = soft_id
self.base_params = {
'user': self.username,
'pass2': self.password,
'softid': self.soft_id,
}
self.headers = {
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
}
def PostPic(self, im, codetype):
"""
im: 图片字节
codetype: 题目类型 参考 http://www.chaojiying.com/price.html
"""
params = {
'codetype': codetype,
}
params.update(self.base_params)
files = {'userfile': ('ccc.jpg', im)}
r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
return r.json()
def ReportError(self, im_id):
"""
im_id:报错题目的图片ID
"""
params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
return r.json()
if __name__ == '__main__':
chaojiying = Chaojiying_Client('超级鹰用户名', '超级鹰用户名的密码', '96001')
im = open('code.jpg', 'rb').read()
print(chaojiying.PostPic(im, 1902))
|