码迷,mamicode.com
首页 > 编程语言 > 详细

python 爬虫学习

时间:2020-05-05 10:55:57      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:||   获取   响应   range   int   connect   gecko   waf   ast   

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 

---

 

python 爬虫学习

标签:||   获取   响应   range   int   connect   gecko   waf   ast   

原文地址:https://www.cnblogs.com/oscarli/p/12829574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!