更多优质内容
请关注公众号

Python爬虫入门(一) 使用requests模块获取网页-张柏沛IT博客

正文内容

Python爬虫入门(一) 使用requests模块获取网页

栏目:Python 系列: 发布时间:2019-10-30 20:34 浏览量:5550

本文开始介绍如何使用python编写网页爬虫,本节主要讲解使用requests模块进行网络请求和下载网页

首先命令行下安装requests模块

pip install requests

  

接下来正式向大家介绍requests模块的用法

例子1:请求一个网页并获取其网页代码内容 

import requests

url = "http://www.baidu.com"

#发送请求,直接返回一个对象,里面包含状态码和内容等;
r = requests.get(url)  

print(r.status_code) #状态码
print(r.text) #内容
print(r.content) #内容
print(r.encoding) #编码方式

PS:使用requests库的一个好处是,没有 https报错问题;如果你用的是urllib2来进行请求https的网页就很可能会报错

上面的代码中:r.content返回页面的内容,但是如果页面有中文,它返回的是bytes类型而不是字符串类型,所以还要decode一下:r.content.decode();而r.text则是直接返回字符串类型 

 

#例子2:请求网页并传入参数

url2="http://httpbin.org/get"  #这个网站可以使用任何的方式请求,方便练习
params = {"k1":"v1","k2":"v2"}
r=requests.get(url2,params)
print(r.url)  #会返回包括参数的完整的url

 

#如果需要添加header信息,比如添加浏览器User-agent的信息,可以传入headers参数

headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'
}
r=requests.get(url,headers=headers)

  

#例3:处理json数据

下面请求一个网址,这个网址返回的是json字符串形式的内容。内容形如:

{"message":"Hello there, wayfaring stranger. If you’re reading this then you probably didn’t see our blog post a couple of years back announcing that this API would go away: http://git.io/17AROg Fear not, you should be able to get what you need from the shiny new Events API instead.","documentation_url":"https://developer.github.com/v3/activity/events/#list-public-events"}

请求代码如下:  

import requests 
r = requests.get("https://github.com/timeline.json")
print(r.json)  #获取内容的json对象
print(r.text)  #json的str格式,但是内容要json解码才行
 
#如果想看到json的内容数据而不是一个json对象
print(r.json())  #返回的是一个字典

一般来说,如果是获取页面的HTML那么就没有必要获取r.json(),直接用r.text获取即可;
如果是请求的接口而不是页面,那么就无需使用r.text而是直接使用r.json()

 

#例4:请求并且下载图片 

下面是一张美女的图片

import requests

from io import BytesIO  #要引入这个包

#拿图片做例子,所以引入这个包 
from PIL import Image

#这是一个美女图片的地址
url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1548577493362&di=3520d8eb40728bb1aed958bdf59f7d45&imgtype=0&src=http%3A%2F%2Fpic2.16pic.com%2F00%2F10%2F77%2F16pic_1077775_b.jpg"

r=requests.get(url)

image = Image.open(BytesIO(r.content))

image.save("meinv.jpg")

 

如果你不想引入io,PIL模块,可以直接将请求到的内容:r.content写入到文件中,注意因为是图片必须要以二进制形式打开再写入,否则会报错

如下:

import requests

#这是一个美女图片的地址
url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1548577493362&di=3520d8eb40728bb1aed958bdf59f7d45&imgtype=0&src=http%3A%2F%2Fpic2.16pic.com%2F00%2F10%2F77%2F16pic_1077775_b.jpg"
r=requests.get(url)

with open("meinv.jpg","wb") as f:
	f.write(r.content)

 

#例5:使用post请求提交表单  

form = {"name":"zbp","password":"123456"}
r = requests.post("http://httpbin.org/post",data=form)
print(r.text)

#这里要说一下,post的data参数,如果你传的是一个字典,那么他会认为你提交的是一个form表单;
但是如果你提交的是一个字符串,那么他认为你是普通的数据

利用post请求可以使用requests来登陆别人的网站,在登陆之后获取网页的cookie信息下次就可以自动登陆,来爬取一些登陆之后才能爬到的信息

 

再说一下httpbin.org这个神奇的网站。
你在浏览器上面访问:
http://httpbin.org/get?a=1

这个网站展现的内容是:
{
  "args": {
    "a": "1"
  }, 
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 
    "Accept-Encoding": "gzip, deflate, sdch", 
    "Accept-Language": "zh-CN,zh;q=0.8", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "Upgrade-Insecure-Requests": "1", 
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"
  }, 
  "origin": "223.74.152.108", 
  "url": "http://httpbin.org/get?a=1"
}

 

而且这个网址支持各种类型的请求,是一个能很好的供大家测试的网址

 

#例6:处理cookie

如果是要获取某个页面的cookie信息可以如下 

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

#获取页面的cookie信息
cookies = r.cookies  #返回一个字典

for k,v in cookies.get_dict().get_items():
	print(k,v)

 

如果请求某页面想带上cookie信息可以如下 

url="http://httpbin.org/cookies"
cookies = {"id":"22","user"=>"zbp"}
r=requests.get(url,cookies=cookies)
print(t.text)

 

requests请求时写入cookie有两种方式:
如果你的cookie是字符串形式:如a=1;b=2;c=3
则cookie可以直接写在headers中:

cookies = "a=1;b=2;c=3"
headers = {
	"Cookie":cookies
}
requests.get(url,headers=headers)

 

如果你的cookie是字典的形式可以通过get()或者post()的cookies参数传入: 

cookies={
	"a":1,
	"b":2,
	"c":3
}
requests.get(url,cookies=cookies)

  

#例7:使用 代理 

代理有很多用途,比如你的ip访问不了某个网站,但是你可以访问得了某个代理ip,这个代理ip又可以访问你想访问的那个网站,于是你可以先请求这个代理ip委托它获取到你想访问的页面然后返回给你 

又或者是,在爬取有些网站的时候,爬取的次数过多会导致网站的封禁,此时使用多个代理ip访问就可以解决这个问题

proxies = {"http":"23.225.228.2:90","https":"23.225.166.3:80"}
r = requests.get(url,proxies=proxies)

 

 




更多内容请关注微信公众号
zbpblog微信公众号

如果您需要转载,可以点击下方按钮可以进行复制粘贴;本站博客文章为原创,请转载时注明以下信息

张柏沛IT技术博客 > Python爬虫入门(一) 使用requests模块获取网页

热门推荐
推荐新闻