首页 > 编程语言 >python 爬虫 几例

python 爬虫 几例

时间:2022-12-05 20:59:33浏览次数:31  
标签:传参 get python 几例 爬虫 print requests response

爬取强大的BD页面,打印页面信息

# 第一个爬虫示例,爬取百度页面

import requests #导入爬虫的库,不然调用不了爬虫的函数

response = requests.get("http://www.1xuni.cn") #生成一个response对象

response.encoding = response.apparent_encoding #设置编码格式

print("状态码:"+ str( response.status_code ) ) #打印状态码

print(response.text)#输出爬取的信息

 

常用方法之get方法实例,下面还有传参实例

# 第二个get方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

response = requests.get("http://httpbin.org/get") #get方法

print( response.status_code ) #状态码

print( response.text )

 

常用方法之post方法实例,下面还有传参实例

# 第三个 post方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

response = requests.post("http://httpbin.org/post") #post方法访问

print( response.status_code ) #状态码

print( response.text )

 

put方法实例

# 第四个 put方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

response = requests.put("http://httpbin.org/put") # put方法访问

print( response.status_code ) #状态码

print( response.text )

 

常用方法之get方法传参实例(1)

如果需要传多个参数只需要用&符号连接即可如下

# 第五个 get传参方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

response = requests.get("http://httpbin.org/get?name=hezhi&age=20") # get传参

print( response.status_code ) #状态码

print( response.text )

 

常用方法之get方法传参实例(2)

params用字典可以传多个

# 第六个 get传参方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

data = {
"name":"hezhi",

"age":20

}

response = requests.get( "http://httpbin.org/get" , params=data ) # get传参

print( response.status_code ) #状态码

print( response.text )

 

常用方法之post方法传参实例(2)

# 第七个 post传参方法实例

import requests #先导入爬虫的库,不然调用不了爬虫的函数

data = {
"name":"hezhi",

"age":20

}

response = requests.post( "http://httpbin.org/post" , params=data ) # post传参

print( response.status_code ) #状态码

print( response.text )



标签:传参,get,python,几例,爬虫,print,requests,response
From: https://www.cnblogs.com/henryZ15/p/16394850.html

相关文章