python爬虫之urllib
#coding=utf-8
#urllib操作类 import time
import urllib.request
import urllib.parse
from urllib.error import HTTPError, URLError
import sys
class myUrllib: @staticmethod
def get_headers(headers):
default_headers = {
'User-Agent': r'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
#'Referer': r'http://www.baidu.com/',
'Connection': 'keep-alive',
'Cookie':'uuid_tt_dd=2845574184150781887; _ga=GA1.2.1608505838; dc_tos=p308'
}
headers = headers and dict(default_headers,**headers) or default_headers
return headers @staticmethod
def get(url,headers={}):
headers = myUrllib.get_headers(headers)
#data=urllib.parse.urlencode(query_data).encode('utf-8')
#r/R:非转义的原始字符串
#u/U:表示unicode字符串
#b:bytes
url=r'%s'%url
request = urllib.request.Request(url,headers=headers,method='GET')
try:
html = urllib.request.urlopen(request).read()
page = html.decode('utf-8')
except HTTPError as e:
print (e.code,e.reason)
except URLError as e:
print (e.reason)
return page @staticmethod
def post(url,data={},headers={}):
headers = myUrllib.get_headers(headers)
data=urllib.parse.urlencode(data)
binary_data=data.encode('utf-8')
url=r'%s'%url
request=urllib.request.Request(url,data=binary_data,headers=headers,method='POST')#发送请求,传送表单数据
# response=urllib.request.urlopen(request)#接受反馈的信息
# data=response.read()#读取反馈信息
# data=data.decode('utf-8')
#print (data.encode('gb18030'))
#print (response.geturl())#返回获取的真实的URL
#info():返回一个对象,表示远程服务器返回的头信息。
#getcode():返回Http状态码,如果是http请求,200表示请求成功完成;404表示网址未找到。
#geturl():返回请求的url地址。 try:
html = urllib.request.urlopen(request).read()
page = html.decode('utf-8')
except HTTPError as e:
print (e.code,e.reason)
except URLError as e:
print (e.reason)
return page getInfo = myUrllib.get('http://localhost:88/test/c.php?act=category',{'Referer': r'https://www.baidu.com/'})
print(getInfo) sys.exit() postInfo = myUrllib.post('http://localhost:88/test/c.php',{'id':1010},{'Referer': r'https://www.baidu.com/'})
print(postInfo)
d:\python\crawler>python urllib01.py
HTTP_HOST:
localhost:88
HTTP_USER_AGENT:
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/63.0.3239.108 Safari/537.36
HTTP_COOKIE:
uuid_tt_dd=2845574184150781887; _ga=GA1.2.1608505838; dc_tos=p308
HTTP_REFERER:
https://www.baidu.com/
REQUEST_METHOD:
GET
GET DATA:
array(1) {
["act"]=>
string(8) "category"
}
#设置代理
#coding=utf-8
import urllib.request
import random
from urllib.error import HTTPError, URLError def proxy_handler(url,iplist,wfile):
#ip = random.choice(iplist)
for ip in iplist:
try:
print('*'*20,'\n ip:',ip)
proxy_support = urllib.request.ProxyHandler({'http':ip})
opener = urllib.request.build_opener(proxy_support)
opener.addheaders = [('User-Agent',r'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36')]
urllib.request.install_opener(opener)
response = urllib.request.urlopen(url)
code = response.getcode()
url = response.geturl()
print('*'*20,'\n url:',url)
print('*'*20,'\n code:',code)
info = response.info()
print('*'*20,'\n info:',info)
if code == 200:
page = response.read()
#写入文件
page = str(page, encoding='utf-8')
fw = open(wfile,'w',encoding='UTF-8')
fw.write(page)
fw.close()
print('*'*20,'\n write file:',wfile)
break
except HTTPError as e:
print (e.code,e.reason)
continue
except URLError as e:
print (e.reason)
continue url = r'http://ip.chinaz.com/'
iplist = ['182.42.244.169:808','122.72.18.34:80','52.44.16.168:3129']
wfile = 'page.txt'
proxy_handler(url,iplist,wfile)
d:\python\crawler>python proxy01.py
********************
ip: 182.42.244.169:808
[WinError 10061] 由于目标计算机积极拒绝,无法连接。
********************
ip: 122.72.18.34:80
********************
url: http://ip.chinaz.com/
********************
code: 200
********************
info: Cache-Control: private
Content-Length: 33900
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Set-Cookie: qHistory=aHR0cDovL2lwLmNoaW5hei5jb20rSVAv5pyN5Yqh5Zmo5Zyw5Z2A5p+l6K
i; domain=.chinaz.com; expires=Tue, 05-Feb-2019 15:03:42 GMT; path=/
X-Powered-By: ASP.NET
Date: Mon, 05 Feb 2018 15:03:42 GMT
X-Cache: MISS from GD-SZ-WEB-01
X-Cache-Lookup: MISS from GD-SZ-WEB-01:80
Connection: close
********************
write file: page.txt
python爬虫之urllib的更多相关文章
- Python爬虫之urllib模块2
Python爬虫之urllib模块2 本文来自网友投稿 作者:PG-55,一个待毕业待就业的二流大学生. 看了一下上一节的反馈,有些同学认为这个没什么意义,也有的同学觉得太简单,关于Beautiful ...
- Python爬虫之urllib模块1
Python爬虫之urllib模块1 本文来自网友投稿.作者PG,一个待毕业待就业二流大学生.玄魂工作室未对该文章内容做任何改变. 因为本人一直对推理悬疑比较感兴趣,所以这次爬取的网站也是平时看一些悬 ...
- python爬虫之urllib库(三)
python爬虫之urllib库(三) urllib库 访问网页都是通过HTTP协议进行的,而HTTP协议是一种无状态的协议,即记不住来者何人.举个栗子,天猫上买东西,需要先登录天猫账号进入主页,再去 ...
- python爬虫之urllib库(二)
python爬虫之urllib库(二) urllib库 超时设置 网页长时间无法响应的,系统会判断网页超时,无法打开网页.对于爬虫而言,我们作为网页的访问者,不能一直等着服务器给我们返回错误信息,耗费 ...
- python爬虫之urllib库(一)
python爬虫之urllib库(一) urllib库 urllib库是python提供的一种用于操作URL的模块,python2中是urllib和urllib2两个库文件,python3中整合在了u ...
- Python爬虫之urllib.parse详解
Python爬虫之urllib.parse 转载地址 Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url 解析url( urlparse() ) ur ...
- Python爬虫之Urllib库的基本使用
# get请求 import urllib.request response = urllib.request.urlopen("http://www.baidu.com") pr ...
- python爬虫之urllib库
请求库 urllib urllib主要分为几个部分 urllib.request 发送请求urllib.error 处理请求过程中出现的异常urllib.parse 处理urlurllib.robot ...
- Python爬虫系列-Urllib库详解
Urllib库详解 Python内置的Http请求库: * urllib.request 请求模块 * urllib.error 异常处理模块 * urllib.parse url解析模块 * url ...
- python爬虫之urllib库介绍
一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib. ...
随机推荐
- linux上安装memcached步骤
libevent: http://libevent.org/ 服务器端:https://code.google.com/archive/p/memcached/downloads 客户端: http: ...
- inode引起的Linux无法创建新文件,磁盘空间不足
df -h,判断硬盘空间是否已经满了,占用率达100% ,就可以断定该分区满了. df -ia,占用率达100%,也会导致无法创建新文件.一般都是存在大量小文件引起的. inode包含文件的元信息,具 ...
- vue1 & vue2 数据驱动更新视图机制对比
vue1 小粒度更新,精确追踪到数据变化所影响的dom变化,精确更新变化的dom 具体实现为,维护 observer watcher directive 三个类 ·observer负责监听数据变化,并 ...
- c# 抽象类 抽象函数 接口
抽象类与抽象方法: 被abstract关键字修饰的类叫做抽象类 被abstract关键字修饰的方法叫做抽象方法 1.抽象方法必须放在抽象类中 2.抽象方法不可以实现代码,用空语句替代 3.抽象方法可以 ...
- Sql server 编写99乘法表
Sql 组织编写语句 declare @one int,@tow int,@str varchar(100),@num intselect @one=1while(@one<=9)beginse ...
- Linux配置Supervisor 配置遇到的坑
在linux中web 应用部署到线上后之后发现退出终端后网站就无法访问了 所以需要用Supervisor来守护进程,它可以保证应用一直处于运行状态,在遇到程序异常.报错等情况,导致 web 应用终止时 ...
- !!!常用JS代码块 (jquery)
jquery代码块 <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> ...
- spring 之 注入之 by name or by type, or both ?
@Autowired 和 @Qualifier 使用xml 注入的时候, 我们可以指定 autowire=“byType” 或“byName” . 但是使用 注解的时候, @Autowired 只 ...
- JavaScript学习-4——DOM对象、事件
本章目录 --------window对象 --------document对象 --------事件 一.window对象 函数调用: 自己封装的函数只写:函数名(): 数学函数Math 例:绝对值 ...
- js实现图片上传预览功能,使用base64编码来实现
实现图片上传的方法有很多,这里我们介绍比较简单的一种,使用base64对图片信息进行编码,然后直接将图片的base64信息存到数据库. 但是对于系统中需要上传的图片较多时并不建议采用这种方式,我们一般 ...