urllib(request,error,parse,robotparse)

  request模块

    方法:urlopen()    {read(),readinto(),getheader(name),getheaders(),fileno()等方法,  msg,status,reason,debuglevel,closed 等属性}

       最基本http请求方法,利用它可以模拟浏览器的一个请求发起过程,同时他还带有助力授权验证authentication,重定向redirection,浏览器cookie 以及其他内容。

import urllib.request
response = urllib.request.urlopen("https://www.baidu.com")
print(response.read().decode("utf-8"))
print(type(response)) --->>>
<html>
<head>
<script>
location.replace(location.href.replace("https://","http://"));
</script>
</head>
<body>
<noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript>
</body>
</html> <class 'http.client.HTTPResponse'>

urlopen()

import urllib.request
response = urllib.request.urlopen("https://www.baidu.com")
print(response.getheaders())
print(response.getheader("server"))
print(response.status)

   data 参数(post 请求    get请求没有data )

import urllib.parse
import urllib.request data = bytes(urllib.parse.urlencode({"word": 'hello'}), encoding="utf-8")
response = urllib.request.urlopen("http://httpbin.org/post", data=data)
print(response.read()) ---》
b'{\n "args": {}, \n "data": "", \n "files": {}, \n "form": {\n "word": "hello"\n }, \n "headers": {\n "Accept-Encoding": "identity", \n "Content-Length": "10", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "Python-urllib/3.6"\n }, \n "json": null, \n "origin": "60.218.161.81, 60.218.161.81", \n "url": "https://httpbin.org/post"\n}\n'

    timeout 参数    用于设置超时时间,单位为秒,(通常设置这个超市模块  用来控制一个网页响应时间 如果长时间未响应  就会报错    异常处理跳过它的抓取)

    

import urllib.parse
import urllib.request, urllib.error
import socket try:
response = urllib.request.urlopen("httpS://httpbin.org/get",timeout=0.1)
except urllib.error.URLError as e:
if isinstance(e.reason,socket.timeout):
print('TIME OUT')

·     Request 方法 (在urlopen 的技术处上可以增加 headers={}等信息)

      urllib.request(url,data,headers={},origin_req_host=NONE,unverifiable=Flase,method=NONE)

        

from urllib import request, parse

url = "https://www.taobao.com/post"
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36'}
dict = {'name':'word'}
data= bytes(parse.urlencode(dict),encoding="utf-8") //((需要转成字节流)
req = request.Request(url =url,data=data,headers=headers,method='POST') //(psot 一定要大写)
response=request.urlopen(req)
print(response.read().decode('utf-8')) 也可以:
req = request.request(url =url,data=data,method='POST')
req.add_header('user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36')
 

      高级用法:

基本库使用(urllib,requests)的更多相关文章

  1. requests库和urllib包对比

    python中有多种库可以用来处理http请求,比如python的原生库:urllib包.requests类库.urllib和urllib2是相互独立的模块,python3.0以上把urllib和ur ...

  2. python爬虫04 | 长江后浪推前浪,Reuqests库把urllib库拍在沙滩上

    最近 有些朋友 看完小帅b的文章之后 把小帅b的表情包都偷了 还在我的微信 疯狂发表情包嘚瑟 我就呵呵了 只能说一句 盘他 还有一些朋友 看完文章不点好看 还来催更 小帅b也只能说一句 继续盘他   ...

  3. Python标准库之urllib,urllib2

    urllib模块提供了一些高级接口,用于编写需要与HTTP服务器交互的客户端.典型的应用程序包括从网页抓取数据.自动化.代理.网页爬虫等. 在Python 2中,urllib功能分散在几个不同的库模块 ...

  4. 设置python爬虫IP代理(urllib/requests模块)

    urllib模块设置代理 如果我们频繁用一个IP去爬取同一个网站的内容,很可能会被网站封杀IP.其中一种比较常见的方式就是设置代理IP from urllib import request proxy ...

  5. python的重试库tenacity用法以及类似库retry、requests实现

    介绍 tenacity is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simpli ...

  6. 爬虫基本库的使用---requests库

    使用requests---实现Cookies.登录验证.代理设置等操作 处理网页验证和Cookies时,需要写Opener和Handler来处理,为了更方便地实现这些操作,就有了更强大的库reques ...

  7. Python3爬虫(四)请求库的使用requests

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.基本用法: 1. 安装: pip install requests 2. 例子: import request ...

  8. 模块urllib requests json xml configparser 学习笔记

    发起http请求 获取返回值 返回值是字符串 第三方模块安装 pip install requests 返回值格式 xml  html  jaon json 功能  loads   字符串>&g ...

  9. Python标准库之urllib,urllib2自定义Opener

    urllib2.urlopen()函数不支持验证.cookie或者其它HTTP高级功能.要支持这些功能,必须使用build_opener()函数创建自定义Opener对象. 1. build_open ...

随机推荐

  1. Vijos1035 贪婪的送礼者 [map的应用]

    1.题意:一群人之间每人准备了一些钱互相送(你们好无聊(⊙o⊙)…),数据给出了每人准备的金额与送出的对象,且保证送给每人的金额是平均的,最后要求出每个人收到的比送出的钱多的数目. 2.分析:模拟题, ...

  2. 对EntityViewInfo的理解

    1,EntityViewInfo常常用作bos中接口参数,来做查询用,其中包含了FilterInfo(过滤).Selector(指定属性)以及Sorter(排序)   SelectorItemColl ...

  3. NET Core 3.1 PATCH HTTP 的使用注意事项

    使用Postman请求示例: 一.在Headers要声明请求类型Content-Type 二.body提交要使用raw,且声明为json格式传输 三.如果有authorization验证还需要带上(如 ...

  4. cocos2dx Geometry Size和Rect

    Size 代码都是基础代码不注释了,写一些特别的 1.赋值时可以接收Size和Vec2类型的值,保证的类型的兼容性 2.对运算符进行了重载,可以按照正常的数学逻辑运算 3..可以使用equals对比大 ...

  5. 写代码 Log 也要认真点么?

    Log自然是需要的, 尤其是正式的产品; 但如果只是自己或内部用用的小工具, 也需要认真点吗? 实话说, 自己对 log 总是不上心, 总觉得调试好了, 能跑了, 足以. 所以, 被大妈怼了好几次 l ...

  6. 「UVA1185」Big Number 解题报告

    UVA1185 Big Number In many applications very large integers numbers are required. Some of these appl ...

  7. Fastadmin 如何引入 layui 模块

    FastAdmin基于RequireJS进行前端JS模块的管理,因此如果我们需要再引入第三方JS插件,则必按照RequireJS的规则进行载入.如果你还不了解什么是RequireJS,可以先简单了解下 ...

  8. Spring注解:InitBinder

    注解 InitBinder 是用来初始化绑定器Binder的,而Binder是用来绑定数据的,换句话说就是将请求参数转成数据对象. @InitBinder用于在@Controller中标注于方法,表示 ...

  9. Netty快速入门(08)ByteBuf组件介绍

    前面的内容对netty进行了介绍,写了一个入门例子.作为一个netty的使用者,我们关注更多的还是业务代码.也就是netty中这两种组件: ChannelHandler和ChannelPipeline ...

  10. 简单工厂模式(C++)

    #include <iostream> using namespace std; class Fruit { public : ; }; class Banana :public Frui ...