手机
当前位置:查字典教程网 >脚本专栏 >python >python使用urllib模块和pyquery实现阿里巴巴排名查询
python使用urllib模块和pyquery实现阿里巴巴排名查询
摘要:urllib基础模块的应用,通过该类获取到url中的html文档信息,内部可以重写代理的获取方法复制代码代码如下:classProxyScr...

urllib基础模块的应用,通过该类获取到url中的html文档信息,内部可以重写代理的获取方法

复制代码 代码如下:

class ProxyScrapy(object):

def __init__(self):

self.proxy_robot = ProxyRobot()

self.current_proxy = None

self.cookie = cookielib.CookieJar()

def __builder_proxy_cookie_opener(self):

cookie_handler = urllib2.HTTPCookieProcessor(self.cookie)

handlers = [cookie_handler]

if PROXY_ENABLE:

self.current_proxy = ip_port = self.proxy_robot.get_random_proxy()

proxy_handler = urllib2.ProxyHandler({'http': ip_port[7:]})

handlers.append(proxy_handler)

opener = urllib2.build_opener(*handlers)

urllib2.install_opener(opener)

return opener

def get_html_body(self,url):

opener = self.__builder_proxy_cookie_opener()

request=urllib2.Request(url)

#request.add_header("Accept-Encoding", "gzip,deflate,sdch")

#request.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")

#request.add_header("Cache-Control", "no-cache")

#request.add_header("Connection", "keep-alive")

try:

response = opener.open(request,timeout=2)

http_code = response.getcode()

if http_code == 200:

if PROXY_ENABLE:

self.proxy_robot.handle_success_proxy(self.current_proxy)

html = response.read()

return html

else:

if PROXY_ENABLE:

self.proxy_robot.handle_double_proxy(self.current_proxy)

return self.get_html_body(url)

except Exception as inst:

print inst,self.current_proxy

self.proxy_robot.handle_double_proxy(self.current_proxy)

return self.get_html_body(url)

【python使用urllib模块和pyquery实现阿里巴巴排名查询】相关文章:

Python中使用logging模块代替print(logging简明指南)

python模拟登陆阿里妈妈生成商品推广链接

python基于mysql实现的简单队列以及跨进程锁实例详解

python构造icmp echo请求和实现网络探测器功能代码分享

python使用urllib2模块获取gravatar头像实例

Python中使用 Selenium 实现网页截图实例

python实现问号表达式(?)的方法

python list使用示例 list中找连续的数字

python使用cookie库操保存cookie详解

用python实现批量重命名文件的代码

精品推荐
分类导航