Python网络爬虫 Requests库知识.1期

Requests库比urllib库更为使用,以下为一些常用的知识整理
requests 库

Import requests
//get的使用.传输params信息
response = requests.get(“url”)
print(response.text) //经过自动猜测解码后生成unicode类型,因此可能会生成乱码。需要自行设定解码方式
print(response.content) //byte数据类型,从网络上抓去的数据,没有经过任何处理
print(response.content.decode(‘utf-8’)
print(response.url) //查看完整的url地址
print(response.encoding) //查看响应头部编码方式
printresponse.status_code) //查看响应码

//params接受一个字典或者字符串的查询参数,字典类型自动转换为url编码,不需要urlencode()
kw = {‘wd’: ‘中国‘}
response = requests.get(“http://www.baidu.com/”, params = kw, headers = headers)
with open(‘name.html’, ‘w’, encoding = ‘utf-8’) as fp:
fp.writre(response.content.decode(‘utf-8’))

						//post的使用.传输data信息

import requests
data = {‘first’:‘true’,
‘pn’:‘1’,
‘kd’:‘python’
}
headers = {‘Referer’:‘https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=’,
‘User-Agent’:‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36’
}
response = requests.post('url’)

发布了47 篇原创文章 · 获赞 10 · 访问量 1734

猜你喜欢

转载自blog.csdn.net/Antonio_Salieri/article/details/100006449