Python学习大数据 爬虫Python AI Sql

Python爬虫入门:那个叫做Urllib的库让我们的pytho

2019-01-19  本文已影响12人  1a076099f916
Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

相信你已经摸清了

浏览器各种请求的套路

也知道了怎么在手机上进行请求和返回数据的抓取

进群:700341555可以获取Python爬虫入门资料+视频资料等等!

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

那么接下来我们就开始来使用 python 了

代码 lu 起来

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

那么

怎么用 python 写各种请求呢?

今天要给大家介绍的就是

Urllib

这可是 python 内置的库

有了它

我们写代码就轻松了

腰也不疼了

腿也不酸了

头发也不秃了

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

那么怎么使用 **Urllib **呢?

接下来

就是

学习 python 的正确姿势

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

在 Python 这个内置的 Urllib 库中

有这么 4 个模块

request

request模块是我们用的比较多的

就是用它来发起请求

所以我们重点说说这个模块

error

error模块呢,就是当我们在使用 request 模块遇到错了

就可以用它来进行异常处理

parse

parse模块就是用来解析我们的 URL 地址的,比如解析域名地址啦,URL指定的目录等

robotparser

这个用的就比较少了,它就是用来解析网站的 robot.txt

ok

了解了 urllib 之后

我们就用 python 代码来模拟请求吧

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

打开 pycharm

如果你还不太知道 pycharm 这个开发工具怎么玩的话

你可以看看这玩意

PyCharm使用教程:PyCharm常用技巧指南

当然

你喜欢别的编辑器也无所谓

反正我觉得

Pycharm

就是

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

首先我们来模拟请求百度吧

超简单

request 模块中的 urlopen 方法

首先我们导入 urllib 的请求模块

<pre class="ql-align-center" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import urllib.request
</pre>

我们在浏览器访问百度的时候

对于 python 来说

就是一句代码的事情

<pre class="ql-align-center" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">urllib.request.urlopen('http://www.baidu.com')
</pre>

我们通过 request 模块的 urlopen 方法

直接用 Get 请求方式请求百度了

那么返回的内容就是和浏览器一样的

我们可以打印出来

<pre class="ql-align-center" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">response = urllib.request.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8'))
</pre>

我们执行

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

百度把源码返回给我们了

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

request 的 urlopen 方法

可以传入的参数主要有 3 个

urllib.request.urlopen(url, data=None, [timeout, ]*)

第一个 **url **就是我们请求的链接

比如我们刚刚就请求百度

第二个参数 data

就是专门给我们 post 请求携带参数的

比如我们在登录的时候

可以把用户名密码封装成 data 传过去

在这里的 data 的值我们可以用 byte 的类型传递

第三个参数 **timeout **就是设置请求超时时间

如果等好久服务器都没有给我们返回数据

我们就不鸟他了!

这就是 request 的 urlopen 主要用法。

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

urlopen 好像不错

但是

如果我们要欺骗服务器说我们是浏览器或者手机请求的呢?

这个时候我们需要添加请求头信息

也就是我们上次说的

request header

那么

这个时候

就该让 request 模块中的 Request 方法出场了

这个 Request 方法的参数多一些

主要的参数

urllib.request.Request(url, data=None, headers={}, method=None)

我们除了定义 url 和 data 之外

我们还可以定义请求头信息

urlopen 默认是 Get 请求

当我们传入参数它就为 Post 请求了

而 Request 可以让我们自己定义请求的方式

这样我们就可以使用 Request 来封装我们的请求信息

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

我们来用 Request 玩一下吧

我们来模拟登陆「逼乎」吧

https://biihu.cc/

没错

就是那个山寨知乎的网站

与世界分享你的装逼技巧与见解

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

进入网站进行登录

我们来抓一下数据先

通过 Fiddler 可以看到我们的请求参数

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

这密码不加密的

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

然后我们再看看我们的请求头信息

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

好了

知道这些之后我们就可以来模拟登录了

导入 urllib

<pre class="ql-align-center" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">from urllib import request,parse
import ssl
</pre>

在这里我们还要导入 ssl

因为逼乎这个b用的是 https

我们可以使用 ssl 未经验证的上下文

<pre class="ql-align-center" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">context = ssl._create_unverified_context()
</pre>

接着定义一下我们的请求 url 和 header

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">url = 'https://biihu.cc//account/ajax/login_process/'
headers = {

假装自己是浏览器

'User-Agent':' Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
}
</pre>

再定义一下我们的请求参数

<pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">dict = {
'return_url':'https://biihu.cc/',
'user_name':'xiaoshuaib@gmail.com',
'password':'123456789',
'_post_type':'ajax',
}
</pre>

我们把请求的参数转化为 byte

<pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">data = bytes(parse.urlencode(dict),'utf-8')
</pre>

然后我们就可以封装 request 了

<pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">req = request.Request(url,data=data,headers=headers,method='POST')
</pre>

最后我们进行请求

<pre class="ql-align-justify" style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">response = request.urlopen(req,context=context)
print(response.read().decode('utf-8'))
</pre>

ok

这样我们就模拟登录了

运行一下看看

{"rsm":{"url":"https://biihu.cc/home/first_login-TRUE"},"errno":1,"err":null}

可以看到我们成功登录逼乎了

Python爬虫入门:那个叫做Urllib的库让我们的python假装是浏览器

ok

以上就是 Urllib 的常用使用方法了

(完)

上一篇 下一篇

猜你喜欢

热点阅读