当前位置:   article > 正文

python爬虫实例100例-Python爬虫 实例

python爬虫实例100例

基本GET请求1. 最基本的GET请求可以直接用get方法

response = requests.get("http://www.baidu.com/")

2. 添加 headers 和 查询参数

如果想添加 headers,可以传入headers参数来增加请求头中的headers信息。如果要将参数放在url中传递,可以利用 params 参数。

kw = {'wd':'长城'}

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}

# params 接收一个字典或者字符串的查询参数,字典类型自动转换为url编码,不需要urlencode()

response = requests.get("http://www.baidu.com/s?", params = kw, headers = headers)

# 查看响应内容,response.text 返回的是Unicode格式的数据

print response.text

2. 传入data数据

对于 POST 请求来说,我们一般需要为它增加一些参数。那么最基本的传参方法可以利用 data 这个参数。

formdata = {

"type":"AUTO",

"i":"i love python",

"doctype":"json",

"xmlVersion":"1

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/954718
推荐阅读
相关标签
  

闽ICP备14008679号