当前位置: 首页 > news >正文

企业官网型网站建设yandex搜索引擎入口

企业官网型网站建设,yandex搜索引擎入口,开发一个游戏软件多少钱,青岛住房和城乡建设厅网站Python urllib Python urllib 库用于操作网页 URL,并对网页的内容进行抓取处理。 本文主要介绍 Python3 的 urllib。 urllib 包 包含以下几个模块: urllib.request - 打开和读取 URL。urllib.error - 包含 urllib.request 抛出的异常。urllib.parse …

Python urllib

Python urllib 库用于操作网页 URL,并对网页的内容进行抓取处理。

本文主要介绍 Python3 的 urllib。

urllib 包 包含以下几个模块:

  • urllib.request - 打开和读取 URL。
  • urllib.error - 包含 urllib.request 抛出的异常。
  • urllib.parse - 解析 URL。
  • urllib.robotparser - 解析 robots.txt 文件。

在这里插入图片描述

urlopen 语法

urllib.request.urlopen(url,data=None,[timeout,]*,cafile=None,capath=None,cadefault=False,context=None)
#url:访问的网址
#data:额外的数据,如header,form data

用法

# request:GET
import urllib.request
response = urllib.request.urlopen('http://www.baidu.com')
print(response.read().decode('utf-8'))# request: POST
# http测试:http://httpbin.org/
import urllib.parse
import urllib.request
data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf8')
response = urllib.request.urlopen('http://httpbin.org/post',data=data)
print(response.read())# 超时设置
import urllib.request
response = urllib.request.urlopen('http://httpbin.org/get',timeout=1)
print(response.read())import socket
import urllib.request
import urllib.errortry:response = urllib.request.urlopen('http://httpbin.org/get',timeout=0.1)
except urllib.error.URLError as e:if isinstance(e.reason,socket.timeout):print('TIME OUT')

响应

# 响应类型
import urllib.open
response = urllib.request.urlopen('https:///www.python.org')
print(type(response))
# 状态码, 响应头
import urllib.request
response = urllib.request.urlopen('https://www.python.org')
print(response.status)
print(response.getheaders())
print(response.getheader('Server'))

Request

声明一个request对象,该对象可以包括header等信息,然后用urlopen打开。

# 简单例子
import urllib.request
request = urllib.request.Requests('https://python.org')
response = urllib.request.urlopen(request)
print(response.read().decode('utf-8'))# 增加header
from urllib import request, parse
url = 'http://httpbin.org/post'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36''Host':'httpbin.org'
}
# 构造POST表格
dict = {'name':'Germey'
}
data = bytes(parse.urlencode(dict),encoding='utf8')
req = request.Request(url=url,data=data,headers=headers,method='POST')
response = request.urlopen(req)
print(response.read()).decode('utf-8')
# 或者随后增加header
from urllib import request, parse
url = 'http://httpbin.org/post'
dict = {'name':'Germey'
}
req = request.Request(url=url,data=data,method='POST')
req.add_hader('User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36')
response = request.urlopen(req)
print(response.read().decode('utf-8'))

设置请求超时

有些请求可能因为网络原因无法得到响应。因此,我们可以手动设置超时时间。当请求超时,我们可以采取进一步措施,例如选择直接丢弃该请求或者再请求一次。

import urllib.requesturl = "http://tieba.baidu.com"
response = urllib.request.urlopen(url, timeout=1)
print(response.read().decode('utf-8'))

使用 data 参数提交数据

在请求某些网页时需要携带一些数据,我们就需要使用到 data 参数。

import urllib.parse
import urllib.requesturl = "http://127.0.0.1:8000/book"
params = {'name':'浮生六记','author':'沈复'
}data = bytes(urllib.parse.urlencode(params), encoding='utf8')
response = urllib.request.urlopen(url, data=data)
print(response.read().decode('utf-8'))

params 需要被转码成字节流。而 params 是一个字典。我们需要使用 urllib.parse.urlencode() 将字典转化为字符串。再使用 bytes() 转为字节流。最后使用 urlopen() 发起请求,请求是模拟用 POST 方式提交表单数据。

异常处理

捕获异常,保证程序稳定运行

# 访问不存在的页面
from urllib import request, error
try:response = request.urlopen('http://cuiqingcai.com/index.htm')
except error.URLError as e:print(e.reason)# 先捕获子类错误
from urllib imort request, error
try:response = request.urlopen('http://cuiqingcai.com/index.htm')
except error.HTTPError as e:print(e.reason, e.code, e.headers, sep='\n')
except error.URLError as e:print(e.reason)
else:print("Request Successfully')
# 判断原因
import socket
import urllib.request
import urllib.errortry:response = urllib.request.urlopen('http://httpbin.org/get',timeout=0.1)
except urllib.error.URLError as e:if isinstance(e.reason,socket.timeout):print('TIME OUT')
http://www.mmbaike.com/news/29459.html

相关文章:

  • 网站设计内容网络优化工程师骗局
  • 网站开发合同付款方式pc网站建设和推广
  • 如何查看一个网站是用什么程序做的网络营销相关工作岗位
  • 蒙牛企业网站建设规划书网络营销理论基础
  • 群辉可以做网站服务器吗线下推广的渠道和方法
  • 用php做企业网站的可行性销售方案
  • 免费net网站空间南昌seo代理商
  • 新闻网站模板html写文案接单平台
  • 北京网站建设公司华网天下下站长之家关键词挖掘工具
  • 站酷网如何接单百度电脑版下载安装
  • 谷歌网站怎么做排名网站流量统计查询
  • 专业做家电的网站广州抖音seo
  • 软件工程师报名官网seo外包优化服务商
  • wap网站建设管理制度seo搜索引擎优化求职简历
  • 牡丹江林口县建设局网站优化大师会员兑换码
  • 一级a做爰片免费网站国语东莞关键词排名优化
  • 哪里有专业做网站的seo优化评论
  • 机械毕业论文代做网站线上营销的优势和劣势
  • 建设部执业资格注册中心网站广告推广渠道
  • 网络推广专员主要的工作内容宁波百度推广优化
  • 零基础做动态网站需要多久怎样宣传网站
  • 建网站的大公司厦门零基础学seo
  • 仿站模板网络推广员压力大吗
  • 厦门网站建设是什么意思做一个公司网站要多少钱
  • 日本出线未seo关键词查询工具
  • 东莞土木建筑学会网站百度网站搜索排名
  • 泉州疫情最新数据seo专员工资一般多少
  • 建设银行商城网站怎么做好网络营销推广
  • 网站背景 手机显示不全沐浴露营销软文
  • 常州网站公司深圳seo网站优化公司