from urllib import response,request,parse,error
from http import cookiejar if __name__ == '__main__':
#response = urllib.request.urlopen("http://www.baidu.com")
#print(response.read().decode("utf-8")) #以post形式发送,没有data就是get形式
#请求头
#data = bytes(urllib.parse.urlencode({"word":"hello"}),encoding="utf-8")
#response = urllib.request.urlopen("http://httpbin.org/post",data=data)
#print(response.read()) #时间限制
#response = urllib.request.urlopen("http://www.baidu.com",timeout=0.01)
#print(response.read().decode("utf-8")) #响应处理
#response = urllib.request.urlopen("http://www.python.org")
#print(type(response))
#状态码
#print(response.status)
#相应头
#print(response.getheaders())
#print(response.getheader("Server")) #复杂请求 request
#request = urllib.request.Request("http://python.org")
#response = urllib.request.urlopen(request)
#print(response.read().decode("utf-8")) #请求头
# add_header也可以
"""
url = "http://httpbin.org/post"
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
"Host":"httpbin.org"
}
dict = {
"name":"Germey"
}
data = bytes(parse.urlencode(dict),encoding="utf8")
req = request.Request(url,data,headers,method="POST")
response = request.urlopen(req);
print(response.read())
""" #代理
"""
proxy_header = request.ProxyHandler({
#代理IP
})
opener = request.build_opener(proxy_header)
response = opener.open("http://httpbin.org/get") #cookies(维持登录状态)
cookie = cookiejar.CookieJar()
handler = request.HTTPCookieProcessor(cookie)
opener = request.build_opener(handler)
response = opener.open("http://www.baidu.com")
""" #保存cookies
#MozillaCookieJar,LWPCookieJar #捕捉异常 基本上HTTPError或者URLError
"""
try:
response = request.urlopen("http://amojury.github.io")
except error.URLError as e:
print(e.reason)
""" #URL解析相关 urlparse urlunparse(反解析) urlencode(字典转请求参数)
#result = parse.urlparse("https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=0&rsv_idx=1&tn=baidu&wd=python%20%E6%89%B9%E9%87%8F%E6%B3%A8%E9%87%8A&rsv_pq=f9b1a8b300011700&rsv_t=1252nVpaBhdm%2FEdlsdrPgUxIHLfk4QNB443eSTUKoRcHFx9G09YZi9N9Dvo&rqlang=cn&rsv_enter=1&rsv_sug3=9&rsv_sug1=8&rsv_sug7=101&rsv_sug2=1&prefixsug=python%2520%25E6%2589%25B9%25E9%2587%258F&rsp=0&inputT=10498&rsv_sug4=14994")
#print(result)

Python爬虫-urllib的基本用法的更多相关文章

  1. Python爬虫Urllib库的高级用法

    Python爬虫Urllib库的高级用法 设置Headers 有些网站不会同意程序直接用上面的方式进行访问,如果识别有问题,那么站点根本不会响应,所以为了完全模拟浏览器的工作,我们需要设置一些Head ...

  2. python爬虫---selenium库的用法

    python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...

  3. Python爬虫Urllib库的基本使用

    Python爬虫Urllib库的基本使用 深入理解urllib.urllib2及requests  请访问: http://www.mamicode.com/info-detail-1224080.h ...

  4. python爬虫 urllib模块url编码处理

    案例:爬取使用搜狗根据指定词条搜索到的页面数据(例如爬取词条为‘周杰伦'的页面数据) import urllib.request # 1.指定url url = 'https://www.sogou. ...

  5. python 爬虫 urllib模块 目录

    python 爬虫 urllib模块介绍 python 爬虫 urllib模块 url编码处理 python 爬虫 urllib模块 反爬虫机制UA python 爬虫 urllib模块 发起post ...

  6. python爬虫---urllib库的基本用法

    urllib是python自带的请求库,各种功能相比较之下也是比较完备的,urllib库包含了一下四个模块: urllib.request   请求模块 urllib.error   异常处理模块 u ...

  7. python爬虫 - Urllib库及cookie的使用

    http://blog.csdn.net/pipisorry/article/details/47905781 lz提示一点,python3中urllib包括了py2中的urllib+urllib2. ...

  8. Python爬虫之BeautifulSoup的用法

    之前看静觅博客,关于BeautifulSoup的用法不太熟练,所以趁机在网上搜索相关的视频,其中一个讲的还是挺清楚的:python爬虫小白入门之BeautifulSoup库,有空做了一下笔记: 一.爬 ...

  9. Python爬虫urllib模块

    Python爬虫练习(urllib模块) 关注公众号"轻松学编程"了解更多. 1.获取百度首页数据 流程:a.设置请求地址 b.设置请求时间 c.获取响应(对响应进行解码) ''' ...

随机推荐

  1. tomcat maven插件启动报错tomcat-users.xml cannot be read

    tomcat maven插件启动报错tomcat-users.xml cannot be read [ERROR] Failed to execute goal org.codehaus.mojo:t ...

  2. nova-conductor与AMQP(二)

    源码版本:H版 一.首先看服务的启动脚本 /usr/bin/nova-conductor import sys from nova.cmd.conductor import main if __nam ...

  3. '0','\0',NULL,EOF的区别

    要看是不是一个东西,打印一下即可 printf("%d %d %d %d\n",'0','\0',NULL,EOF); 输出: 48 0 0 -1 结论: '\0'与NULL 都是 ...

  4. 2015年IPC网络摄像机技术发展现状分析

    网络摄像机将图像转换为基于TCP/IP网络标准的数据包,使摄像机所摄的画面通过RJ-45以太网接口或WIFI WLAN无线接口直接传送到网络上,通过网络即可远端监视画面. 一.网络摄像机的基本原理 网 ...

  5. bzoj 1367: [Baltic2004]sequence

    1367: [Baltic2004]sequence Time Limit: 20 Sec  Memory Limit: 64 MB Description Input Output 一个整数R Sa ...

  6. 【BZOJ】2337: [HNOI2011]XOR和路径 期望+高斯消元

    [题意]给定n个点m条边的带边权无向连通图(有重边和自环),在每个点随机向周围走一步,求1到n的期望路径异或值.n<=100,wi<=10^9. [算法]期望+高斯消元 [题解]首先异或不 ...

  7. Shader -> Photoshop图层混合模式计算公式大全

    Photoshop图层混合模式计算公式大全 混合模式可以将两个图层的色彩值紧密结合在一起,从而创造出大量的效果,在这些效果的背后实际是一些简单的数学公式在起作用. 下面是photoshop cs2中所 ...

  8. UTF8字符串转换为汉字 c#

    using System; /// <summary> /// UTF8字符串转换为汉字用的类 /// 转换如"\\u8d35"之类的字符串为对应的汉字 /// < ...

  9. layui的模块化和非模块化使用

    非模块化和模块化的区别是 非模块化不用每次都调用layui.use([],fun...)引入对应模块,引入的JS是/layui/layui.all.js 模块化必须每次都调用layui.use([], ...

  10. Python标准库笔记(5) — sched模块

    事件调度 sched模块内容很简单,只定义了一个类.它用来最为一个通用的事件调度模块. class sched.scheduler(timefunc, delayfunc)这个类定义了调度事件的通用接 ...