《python网络爬虫入门实践》笔记:chp3 静态网页抓取(上)

获取响应内容

import requests

r = requests.get("http://www.baidu.com")
print("文笔编码", r.encoding)
print("响应状态码", r.status_code)
print("字符串方式的响应体", r.text)

定制Requests

传递url参数
import requests

keydict = {'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=keydict)
print("url已经正确编码", r.url)
print("字符串方式的响应体:\n", r.text)

定制请求头
import requests

headers = {
    'Host': 'httpbin.org',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/96.0.4664.45 Safari/537.36 Edg/96.0.1054.29'}
r = requests.get("http://httpbin.org", headers=headers)
print(r.status_code)

发送POST请求
import requests

keydict = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=keydict)
print("url已经正确编码", r.url)
print("字符串方式的响应体:\n", r.text)

超时
import requests

link = "http://www.httpbin.org"
r = requests.get(link, timeout=0.0001)
print("文笔编码", r.encoding)
print("响应状态码", r.status_code)
print("字符串方式的响应体", r.text)
上一篇:PFKS


下一篇:nprogress进度条