Python爬虫——Urllib库使用

Urllib库解析

什么是Urllib

Python内置的HTTP请求库:

urllib.request 请求模块

urllib.error 异常处理模块

urllib.parse url解析模块

urllib.robotparser robots.txt 解析模块

获取URL

最简单的使用 urllib.request 的方法如下:

import urllib.request
with urllib.request.urlopen('http://python.org/') as response:
    html = response.read()
print(html)

以最简单的形式创建一个 Request 对象,该对象指定要获取的 URL。urlopen使用此 Request 对象进行调用会为所请求的 URL 返回一个响应对象。

import urllib.request
req = urllib.request.Request("http://python.org/")
with urllib.request.urlopen(req) as response:
     the_page = response.read()
print(the_page)

数据

Post请求

将数据发送到URL,在 HTML 表单的常见情况下,数据需要以标准方式进行编码,然后作为data 参数传递给 Request 对象。

post请求主要在请求体内以表单的形式发送

import urllib.parse
import urllib.request

url = 'http://www.someserver.com/cgi-bin/register.cgi'
values = {'name': 'Michael Foord', 'location': 'Northampton', 'language': 'Python'}
data = urllib.parse.urlencode(values)       #编码
data = data.encode('ascii')
req = urllib.request.Request(url, data)
with urllib.request.urlopen(req) as response:
    the_page = response.read()
print(the_page)
Get请求

如果不传递data参数,urllib 将使用GET请求。

get请求的数据是放在url中的。

import urllib.request
import urllib.parse

data = {}
data['wd'] = '青岛'
url_values = urllib.parse.urlencode(data)
# 转换格式:wd=%E9%9D%92%E5%B2%9B
print(url_values)
url = 'http://www.baidu.com'
full_url = url + '/s?' + url_values
# 输出 http://www.baidu.com/s?wd=%E9%9D%92%E5%B2%9B
data = urllib.request.urlopen(full_url)
print(data)
headers

浏览器标识自己的方式是通过 User-Agent标头。创建 Request 对象时,您可以传入标头字典。以下示例发出与上述相同的请求,但将自身标识为 Internet Explorer 的版本。

import urllib.parse
import urllib.request

url = 'http://www.someserver.com/cgi-bin/register.cgi'
# 用户唯一标识
user_agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64)" 
values = {'name': 'Michael Foord', 'location': 'Northampton', 'language': 'Python'}
# 传入请求头
headers = {'User-Agent': user_agent}
data = urllib.parse.urlencode(values)  # 编码
data = data.encode('ascii')
req = urllib.request.Request(url, data, headers)
with urllib.request.urlopen(req) as response:
    the_page = response.read()
print(the_page)

处理异常

网址错误

通常,由于没有网络连接(没有到指定服务器的路由)或指定的服务器不存在,会引发 URLError。在这种情况下,引发的异常将具有“原因”属性,它是一个包含错误代码和文本错误消息的元组。

req = urllib.request.Request('https://www.pretend_server.org')
try:
    urllib.request.urlopen(req)
except urllib.error.URLError as e:
    print(e.reason)
HTTP错误

来自服务器的每个 HTTP 响应都包含一个数字“状态代码”。有时状态码表示服务器无法满足请求。默认处理程序将为您处理其中一些响应(例如,如果响应是请求客户端从不同 URL 获取文档的“重定向”,则 urllib 将为您处理)。对于那些它无法处理的,urlopen 会引发一个HTTPError. 典型的错误包括“404”(未找到页面)、“403”(请求被禁止)和“401”(需要身份验证)。

import urllib.request
req = urllib.request.Request('http://www.python.org/fish.html')
try:
    urllib.request.urlopen(req)
except urllib.error.HTTPError as e:
    # 响应码,输出404
    print(e.code)
    print(e.read())

合并:

from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
req = Request('http://www.python.org/fish.html')
try:
    response = urlopen(req)
except HTTPError as e:
    print('The server couldn\'t fulfill the request.')
    print('Error code: ', e.code)
except URLError as e:
    print('We failed to reach a server.')
    print('Reason: ', e.reason)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python爬虫教程urllib主要是介绍了Python爬虫技术urllib的基础用法。该教程详细讲解了urllib的各种功能和方法,包括发送请求、处理响应、设置请求头、处理异常等。这个教程对于想要学习和使用Python进行网络爬虫的人来说具有很大的参考价值。 同时,该教程也提到了一些常见的问题和解决方法,例如模拟超时的处理方法。在网络爬虫,有时候我们长时间无法访问一个页面,可能是因为网速有限或者被发现我们是一个爬虫。针对这种情况,可以使用timeout参数来设置超时时间,如果超过了设定的时间仍无法获取响应,则会抛出URLError异常,我们可以在异常处理对超时进行相应的处理。 总的来说,Python爬虫教程urllib是一个很好的学习资源,适合想要入门或深入学习Python爬虫技术的人使用。它提供了详细的教程和实例,可以让你快速上手和掌握使用urllib进行网络爬虫的基本知识和技巧。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python爬虫urllib基础用法教程](https://download.csdn.net/download/weixin_38656741/12858843)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Python爬虫 —— urllib使用(get/post请求+模拟超时/浏览器)](https://blog.csdn.net/qq_50587771/article/details/123840479)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [python爬虫urllib3的使用示例](https://download.csdn.net/download/weixin_38681147/12867742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值