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. 0-如何正确使用 Django的User Model

    本篇主要讨论一下User Model的使用技巧. 注意, 由于Django 1.5之后user model带来了很大的变化, 本篇内容只针对django 1.5之后的版本. 1. 确定 User Mo ...

  2. Qt undefined reference to ***

    错因:某个类声明了一个函数但是没有定义就直接使用.

  3. mysql 修改密码的几种方式

    第一种方式: 最简单的方法就是借助第三方工具Navicat for MySQL来修改,方法如下: 1.登录mysql到指定库,如:登录到test库. 2.然后点击上方“用户”按钮. 3.选择要更改的用 ...

  4. 【洛谷 P4072】 [SDOI2016]征途(斜率优化)

    好久没写斜率优化板子都忘了, 硬是交了十几遍.. 推一下柿子就能得到答案为 \[m*\sum x^2-(\sum x)^2\] 后面是个定值,前面简单dp,斜率优化一下就行了. \(f[i][j]=f ...

  5. Paramiko使用

    1.下载安装 pycrypto-2.6.1.tar.gz (apt-get install python-dev) 解压,进入,python setup.py build[编译],python set ...

  6. hdu 3729 I'm Telling the Truth(二分匹配_ 匈牙利算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS ( ...

  7. Automation Testing - Best Practice(书写规范)

    Coding Standards Coding Standards are suggestions that will help us to write automation Scripts code ...

  8. Linux内核同步原语之原子操作【转】

    转自:http://blog.csdn.net/npy_lp/article/details/7262388 避免对同一数据的并发访问(通常由中断.对称多处理器.内核抢占等引起)称为同步. ——题记 ...

  9. git@oschina.net源代码管理使用日记【转】

    转自:https://www.cnblogs.com/Juvy/p/3556902.html git的优势: 1 可以创建分支: 2 版本控制是基于每一次提交的,而不需要考虑每次提交了多少个文件. 下 ...

  10. linux设备驱动模型-浅析-转

    1.  typeof typeof并非ISO C的关键字,而是gcc对C的一个扩展.typeof是一个关键字(类似sizeof),用于获取一个表达式的类型. 举个简单的例子: char tt; typ ...