常用方法
常用属性
import requests
r = requests.get("https://baidu.com")
r.content # 响应内容,字节形式
r.text # 响应内容,文本形式
r.status_code # 返回码
r.headers # 响应头
r.request.headers # 请求响应头
下载图片
# 下载图片
import requests
url = "https://gimg3.baidu.com/yule/src=https%3A%2F%2Fsearch-operate.cdn.bcebos.com%2F884c1c1f5db46c170df3c34a8e213ec9.png&refer=http%3A%2F%2Fwww.baidu.com&app=2019&size=w931&n=0&g=0n&q=75&fmt=auto?sec=1654707600&t=04a64789e61785ba387fcd05b4872794"
response = requests.get(url).content
with open("凯尔特人logo.jpg", mode="wb") as f:
f.write(response)
下载成功
响应解码
import requests
r = requests.get("https://baidu.com", headers=header) # 携带headers
print(r.text)
结果:
存在乱码
r.encoding = "utf-8"
print(r.text)
# 或者
print(r.content.decode("utf-8")) # content打印的是字节,通过decode修改编码。 通常在图片,音频视频中使用
修改方法:
两种方法都可以实现
GET带参数+header
url = "https://www.baidu.com/s?ie=UTF-8"
wd = {"wd": "凯文杜兰特"}
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"
}
r = requests.get(url, headers=header, params=wd)
print(r.content.decode("utf-8"))
POST请求带参数+header+cookie
百度翻译案例需要携带cookie,headers再加上post的请求参数
url = "https://fanyi.baidu.com/v2transapi?from=zh&to=en"
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36",
"Cookie": "__yjs_duid=1_86f9126db5ce41a9bf3f0269380c8b761636425271400; BIDUPSID=C71DA73E37B99CFE0450B0BF7F59D78D; PSTM=1636426861; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_SPD_SWITCH=1; SOUND_PREFER_SWITCH=1; APPGUIDE_10_0_2=1; BAIDUID=A2873DD9BDFBD356C7A13C504E310B07:FG=1; BDUSS_BFESS=FNqfm53TnFBWnJwZDlLaEpMTm9ibDJTUjduS2NVenFSYlNaMmhvcGpsZHBTTDFpSUFBQUFBJCQAAAAAAAAAAAEAAABPPfd0ztK7ucrHuPa2vLj4ztIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGm7lWJpu5ViU; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=36555_36454_36166_36569_36075_36520_26350_36299_36468_36314; BA_HECTOR=8hah2004ah8g0l85201h9tttm14; ZFY=hBzia5Ckjb7IDbBAZP5OJapPCqL51pbeRALR4rXhsrE:C; delPer=0; PSINO=5; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1653035521,1653289879,1654076838,1654590405; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1654590405; ab_sr=1.0.1_NTA2NGIwNzcxYjZjMmMwOGNhNzUyYjM2ZjI1ODJjZTA4MTRhOTFkNjQ4ZDk5MDRkNGQzZjA5Yjc5MmFmZDI3ZDU1YmY0OGFhNjViMWNjYjE4NjQyMjdmMmZhZjJjYWRiYTVjNjk4YjYzMDdlYTYyNmRlZDY2MzAwZDY5NDJmMDQ1OWFjNzQ4NjJhYzNhZjllNjJjYmVkMDAyNGNmOTgzZg=="
}
data = {
"from": "zh",
"to": "en",
"query": "你好",
"transtype": "translang",
"simple_means_flag": "3",
"sign": "232427.485594",
"token": "df2d7ae7fe153e1b2e70140ff65bed4c",
"domain": "common"
}
response = requests.post(url=url, headers=header, data=data)
print(response.json())
结果有了
cookie和session
带上cookie和session可以请求登录之后的页面等
url = "https://blog.csdn.net/wangzhuanjia?type=blog"
cookie = "uuid_tt_dd=10_9870182710-1636508622278-266773; UN=wangzhuanjia; Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac=6525*1*10_9870182710-1636508622278-266773!5744*1*wangzhuanjia; _ga_VHSCGE70LW=GS1.1.1644848275.32.1.1644848287.0; _ga=GA1.2.2004775355.1641265359; __gads=ID=0b46c6e8ed8ce08d-22c764c09ed0003b:T=1636594617:RT=1644848292:S=ALNI_MbwzgmGwgcmKJO-hOUnVs-K3VFEDg; Hm_lvt_e5ef47b9f471504959267fd614d579cd=1650877929; UserName=wangzhuanjia; UserInfo=dcbbe1cb229b45b29e43c89a6b6b459f; UserToken=dcbbe1cb229b45b29e43c89a6b6b459f; UserNick=%E7%93%9C%E7%9A%AE%E4%B8%89%E4%B8%89; AU=DF0; BT=1652757085940; p_uid=U010000; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22islogin%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22wangzhuanjia%22%2C%22scope%22%3A1%7D%7D; ssxmod_itna=QqmxBDyiD=qWqDKGHdiQR30K5xgiiqdDgGmoEGx0HpweGzDAxn40iDto=X7QrDrYb4arKGOnNFiS24oeawKGicuodmDB3DEx064WmDYAkDt4DTD34DYDixibCxi5GRD0KkDbxYPcDi4DrfrKDRxi7DDHQ8x07DQv8Olw8VEcGu67CqlHPaAKD9cYDsgiAiA9h+b3eSE4ODlILDCF1uEKCd4Gdb0G3QYhNKY2DZFxYeQ+xZeCP5GGxkWi0e0l4QQ0uYG2X8qxDip2YM+hDD==; ssxmod_itna2=QqmxBDyiD=qWqDKGHdiQR30K5xgiiqdDgGmoEDnF8PrcDDsaiatDL01y/4qOqnRDk8fnuKxkw+ZMK4kq3/L+6Q5H7C20+/CFxhQk1VSTSr42YwCKjRVffhZnswK80Anj0q4mGSXVQDYbzSfsRb3SnM08fw=mm8f8CeA3+lNgeMUlRBthlbuh/ffsYBKqiwbkYgtmR7Q/4p6kl2DtocKsK09ElEWi1oUWcwyv9PyvGBUEF0xFqMt1Bmj=at9Zlo49RRuV998c8lFsnuulf7mGM8EEngaVDdH4DdbMKsYEsSka6z3cjPocWXWgDIa=GrIEY+6YQelfzODMijQYNgBN0WKBYm2Ovjjr7QLKc2hnQmQUIrQHm4DQIPFDvIrbCiaKD4RbdsgWKhQsb=N+Qx08DiQYYD==; c_segment=5; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1654002465,1654069126,1654156523,1654486113; dc_sid=8cf84b0154812c669096936abd0c7a13; c_dl_prid=1653532315990_288447; c_dl_rid=1654516898429_514697; c_dl_fref=https://www.csdn.net/tags/MtTaEgysMTg0NDgzLWJsb2cO0O0O.html; c_dl_fpage=/download/weixin_38632825/14912602; c_dl_um=-; c_first_ref=default; __gpi=UID=000004992a781cfd:T=1649337179:RT=1654569710:S=ALNI_MbqyfwD7NUSd2oO8HgvWZUyt2PyyQ; dc_session_id=10_1654584204178.952353; c_dsid=11_1654584204718.874503; c_first_page=https%3A//blog.csdn.net/weixin_43881394/article/details/114084742; log_Id_click=1504; c_pref=https%3A//blog.csdn.net/wangzhuanjia%3Fspm%3D1010.2135.3001.5343; c_ref=https%3A//blog.csdn.net/wangzhuanjia/article/details/125150733%3Fspm%3D1001.2014.3001.5502; c_page_id=default; dc_tos=rd3nhk; log_Id_pv=2699; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1654592169; log_Id_view=6531"
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36",
"Cookie": cookie
}
session = requests.session()
response = session.get(url=url, headers=header)
cookies = {i.split("=")[0]: i.split("=")[1] for i in cookie.split("; ")}
# response = session.get(url=url, headers=header, cookies=cookies) # 这种方法也可以
print(response.text)
小技巧
cookie对象转换
import requests
response = requests.get("https://www.baidu.com")
print(response.cookies)
print(requests.utils.dict_from_cookiejar(response.cookies))
print(requests.utils.cookiejar_from_dict({'BDORZ': '27315'}))
结果:
<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
{'BDORZ': '27315'}
<RequestsCookieJar[<Cookie BDORZ=27315 for />]>
SSL证书验证
HTTPS = HTTP + SSL 就是http加上证书验证
类似网站
r = requests.get("https://www.12306.cn/index", verify=False)
设置请求超时
r = requests.get("https://www.12306.cn/index", timeout=0.01)
设置请求时长为0.01肯定是不通的,即可看到报错
url编码和解码
url = '''https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E7%BE%8E%E5%9B%BD%E8%81%8C%E4%B8%9A%E7%AF%AE%E7%90%83%E8%81%94%E8%B5%9B&fenlei=256&oq=nba&rsv_pq=8cfe0a2400093003&rsv_t=d6af81dZLJmPNUlsa0NRCsidWMuGkv4Itf0seUurHQjuIOEmV7Iqx8owiSE&rqlang=cn&rsv_dl=tb&rsv_enter=0&rsv_btype=t&inputT=1265&rsv_sug3=11&rsv_sug1=9&rsv_sug7=100&rsv_n=2&rsv_sug4=1265'''
print(requests.utils.unquote(url))
https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=美国职业篮球联赛&fenlei=256&oq=nba&rsv_pq=8cfe0a2400093003&rsv_t=d6af81dZLJmPNUlsa0NRCsidWMuGkv4Itf0seUurHQjuIOEmV7Iqx8owiSE&rqlang=cn&rsv_dl=tb&rsv_enter=0&rsv_btype=t&inputT=1265&rsv_sug3=11&rsv_sug1=9&rsv_sug7=100&rsv_n=2&rsv_sug4=1265
这里看的在清楚一点
print(requests.utils.unquote("%E7%BE%8E%E5%9B%BD%E8%81%8C%E4%B8%9A%E7%AF%AE%E7%90%83%E8%81%94%E8%B5%9B"))
print(requests.utils.quote("美国职业篮球联赛"))
美国职业篮球联赛
%E7%BE%8E%E5%9B%BD%E8%81%8C%E4%B8%9A%E7%AF%AE%E7%90%83%E8%81%94%E8%B5%9B
使用代理
proxies = {
"HTTP": "110.243.30.45:9999",
"HTTPS": "223.199.21.229:9999"
}
r = requests.get(url="https://baidu.com", proxies=proxies)
print(r)