python 爬虫学习
response = requests.get("http://www.baidu.com")
response.content.decode("utf-8") 返回bytes类型 decode解码
response.text request.encoding = "gbk" # 修改编码 返回str类型
获取图片
# coding=utf-8 import requests url = "http://wap.jiapai.net.cn/images/1.jpg" response = requests.get(url) with open("baidu.png","wb") as f: f.write(response.content)
---
# 状态码
response.status_code
# 响应头
response.headers
# 请求头
response.request.headers
200{‘Content-Length‘: ‘20851‘, ‘Content-Type‘: ‘image/jpeg‘, ‘Last-Modified‘: ‘Sun, 28 Jul 2019 04:29:48 GMT‘, ‘Accept-Ranges‘: ‘bytes‘, ‘ETag‘: ‘"1f3f6d17fd44d51:0"‘, ‘Set-Cookie‘: ‘sdwaf-test-item=1ed57f5405075208510954035156575b5c5754065406040d015701515e520c; path=/; HttpOnly‘, ‘X-Powered-By‘: ‘SDWAF‘, ‘Date‘: ‘Tue, 05 May 2020 01:56:48 GMT‘} {‘User-Agent‘: ‘python-requests/2.23.0‘, ‘Accept-Encoding‘: ‘gzip, deflate‘, ‘Accept‘: ‘*/*‘, ‘Connection‘: ‘keep-alive‘}
---
# 发送带header的请求
# coding=utf-8 import requests url = "http://wap.jiapai.net.cn/images/1.jpg" headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36"} response = requests.get(url,headers=headers) print(response.status_code) print(response.headers) print(response.request.headers)
---
# 占位符 建议使用format+ {} 代替
input_string = input("")
url = "http://www.baidu.com/s?wd={}".format(input_string) || url = "https://www.baidu.com/s?wd=%s"%input_string
---
相关推荐
ZHANGRENXIANG00 2020-06-28
kikaylee 2020-05-05
sunzhihaofuture 2020-07-19
sunzhihaofuture 2020-06-06
oXiaoChong 2020-06-05
ARCXIANG 2020-06-05
夜斗不是神 2020-11-17
染血白衣 2020-11-16
ARCXIANG 2020-11-02
ARCXIANG 2020-10-28
CycloneKid 2020-10-27
荒谬小孩 2020-10-26
逍遥友 2020-10-26
snakeson 2020-10-09
meylovezn 2020-08-28
囧芝麻 2020-08-17
数据挖掘工人 2020-08-15
cxcxrs 2020-07-28