爬虫:python中的requests简单使用

一、requests.get()使用

#Python 的标准库 urllib 提供了大部分 HTTP 功能,但使用起来较繁琐。
#通常,我们会使用另外一个优秀的第三方库:Requests,它的标语是:Requests: HTTP for Humans。
import requests
reponse=requests.get("https://www.baidu.com/")
reponse.encoding='utf-8'
# 返回状态码
print(reponse.status_code)
# 打印网页的内容
print(reponse.text)

二、请求头

import requests
#构建请求头,模拟浏览器访问
header={
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'
}
reponse=requests.get('http://www.baidu.com/',headers=header)
print(reponse.content.decode('utf-8'))

三、post请求

import requests
#包装要发送的数据
data={
    'name':'zhang',
    'age':'18'
}
header={
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36'
}
#调用post()方法进行post请求
reponse=requests.post('http://httpbin.org/post',data=data,headers=header)
print(reponse.text)

返回结果如下:传递的name和age的值已经被接收
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "age": "18", 
    "name": "zhang"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "17", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36"
  }, 
  "json": null, 
  "origin": "180.173.37.153", 
  "url": "http://httpbin.org/post"
}

四、代理的使用

import requests
proxy={
    'http':'122.114.31.177'
}
reponse=requests.get('http://www.baidu.com/',proxies=proxy)
print(reponse.status_code)

五、超时设置

from requests.exceptions import Timeout
try:
    reponse=requests.get('http://www.baidu.com/',timeout=0.001)
    print(reponse.status_code)
except Timeout:
    print("访问超时")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值