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. vagrant在windows下的安装和配置(一)

    记录一下安装和配置过程中的一些坑步骤一分别下载vagrant和VirtualBox,我这里下载的是vagrant_1.9.1.msi 和 VirtualBox-5.1.14-112924-Win.ex ...

  2. win7+oracle11,vmbox中winxp连接

    重启TNSLSNR  打开CMD,输入以下命令. lsnrctl stop lsnrctl start lsnrctl stat /////////////////////////////////// ...

  3. $CH5501$ 环路运输 环形$+$单调队列

    CH Description 在一条环形公路旁均匀地分布着N座仓库,编号为1~N,编号为 i 的仓库与编号为 j 的仓库之间的距离定义为 dist(i,j)=min⁡(|i-j|,N-|i-j|),也 ...

  4. tomcat服务器基本操作:实现www.baidu.com访问tomcat中项目

    0.实现用其他的域名,而不再使用localhost:8080/xxx,访问tomcat中的项目: (1). 修改访问tomcat的端口号: (2). tomcat配置虚拟主机: (3). 本地DNS解 ...

  5. 现代主流框架路由原理 hash、history的底层原理

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. NetCore 启动地址配置详解

    背景 程序在发布部署时候,设置环境ASPNETCORE_URLS不生效,也没在代码里使用UseUrls("xxxx"),启动一直是http://localhost:5000.最后测 ...

  7. Go语言教程之结构体

    Hello,大家好,我是小栈君,最近因为工作的事情延误了一点分享的进度,但是我会尽量抽时间分享关于IT干货知识,还希望大家能够持续关注"IT干货栈"哦. 闲话不多说,今天给大家继续 ...

  8. eclipse中使用postgreSQL报错( Cannot load JDBC driver class )

    需求: 使用Maven插件调用PostgreSQL数据库 环境: eclipse_4.5.0+JDK_1.7+Tomcat_7.0+Maven+postgresql-9.1-901.jdbc4.jar ...

  9. C语言之数组用法总结

    一维数组的定义:1.数组的数据类型:每一元素占内存空间的字节数.2.数组的存储类型:内存的动态. 静态存储区或CPU的寄存器.3.一维数组在内存中占用的字节数为:数组长度X sizeof (基类型). ...

  10. 如何用visual studio code更好的编写python

    目录 1.先决条件 2.Visual Studio Code扩展安装Python 3.Visual Studio Code扩展安装Python for VSCode 4.Visual Studio C ...