跳至主要内容

博文

目前显示的是标签为“爬虫”的博文

python 之 Requests

https://requests.readthedocs.io/zh_CN/latest/user/quickstart.html 使用  r.content  来找到编码,然后设置  r.encoding  为相应的编码。这样就能使用正确的编码解析  r.text  了 import requests url = 'https://www.baidu.com' # 传递 URL 参数 # 想为 URL 的查询字符串(query string)传递某种数据。如果你是手工构建 URL,那么数据会以键/值对的形式置于 URL 中,跟在一个问号的后面。例如, httpbin.org/get?key=val # Requests 允许你使用 params 关键字参数,以一个字符串字典来提供这些参数 payload = {'wd':'python'} r = requests.get(url, params=payload) print(dir(r)) print(r.url) print(r.content) print(r.encoding) print(r.text) print(r.headers)   #以一个 Python 字典形式展示的服务器响应头 # r.headers['Content-Type'] # r.headers.get('content-type') print(r.cookies) with open("requests_results.html", "wb") as f:     f.write(r.content)      print(r.json) # 检查请求是否成功 print(r.raise_for_status()) print(r.status_code)  #响应状态码 # 将文本流保存到文件 filename, chunk_size with open(filename, 'wb') as fd:     for chunk in r.iter_content(chunk_size):         fd.write(chu...