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

国家时事新闻长沙企业seo服务

国家时事新闻,长沙企业seo服务,常用的网站推广,网站推广软件价格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.yayakq.cn/news/490357/

相关文章:

  • 合肥做企业网站的网络公司网站怎么做出来的
  • 成都市建设学校网站属于网站建设过程规划
  • 长春火车站在哪权威的网站制作
  • asp一个空间建多个网站系统怎么查找网站
  • 网站建设合同细节常州网站建设
  • 中小企业网站建设 论文在线A视频网站(级做爰片)
  • 做网站参考文献企查查企业信息查询官网登录入口
  • 带数据库的网站模板买卖平台有哪些网站
  • 社团网站设计网页创建网站能赚钱吗
  • asp网站实例网站制作都有哪些
  • 盐城做企业网站多少钱wordpress 搬家乱码
  • 东平网站制作哪家好交易平台网站程序
  • 目前哪些企业需要做网站建设的呢科技画
  • 做企业网站需要多少钱湖北seo优化诊断
  • 网站蓝色域名免费
  • 论坛网站怎么推广企业为什么要分析环境
  • 网站上推广游戏怎么做网站集约化建设工作方案
  • 建设一个连接的网站怎么把自己做的网站让外网访问
  • qq登录网站怎么做广告制作包括哪些项目
  • h5响应式网站设计方案中企动力双语网站
  • 解释网站为什么这样做宿迁定制网站建设
  • 可以免费建设网站区块链插件wordpress
  • 网站的域名做邮箱wordpress设置上传大小
  • 郑州网站建设服务毕业设计网页设计题目
  • 免费建站系统开源企业网站备案 过户
  • 网站建设技术可行性分析uc官方网站开发中心
  • 在网站制作意见征集是怎么做的长沙公司网络推广
  • 网站建设 中企动力厨具商城网站商家入驻功能
  • 成都建工路桥建设有限公司网站郑州优化公司有哪些
  • 哪个网站有摄影作品网站活动策划方案