python 爬虫完美伪装

#coding:utf-8
'''
Created on 2012-6-25
@author: lzs
''' 

import random
import socket
import urllib2
import cookielib

ERROR = {
        '0':'Can not open the url,checck you net',
        '1':'Creat download dir error',
        '2':'The image links is empty',
        '3':'Download faild',
        '4':'Build soup error,the html is empty',
        '5':'Can not save the image to your disk',
    }

class BrowserBase(object): 

    def __init__(self):
        socket.setdefaulttimeout(20)

    def speak(self,name,content):
        print '[%s]%s' %(name,content)

    def openurl(self,url):
        """
        打开网页
        """
        cookie_support= urllib2.HTTPCookieProcessor(cookielib.CookieJar())
        self.opener = urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
        urllib2.install_opener(self.opener)
        user_agents = [
                    'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
                    'Opera/9.25 (Windows NT 5.1; U; en)',
                    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
                    'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
                    'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
                    'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
                    "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Ubuntu/11.04 Chromium/16.0.912.77 Chrome/16.0.912.77 Safari/535.7",
                    "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0 ",

                    ] 
       
        agent = random.choice(user_agents)
        self.opener.addheaders = [("User-agent",agent),("Accept","*/*"),('Referer',' http://www.google.com')]
        try:
            res = self.opener.open(url)
            print res.read()
        except Exception,e:
            self.speak(str(e)+url)
            raise Exception
        else:
            return res

if __name__=='__main__':
    splider=BrowserBase()
    splider.openurl(' http://blog.csdn.net/v_JULY_v/archive/2010/11/27/6039896.aspx')   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#coding:utf-8 import time import webbrowser as web import os import random #随机选择一个浏览器打开网页 def open_url_use_random_browser(): #定义要访问的地址 url='http://www.baidu.com' #定义浏览器路径 browser_paths=[r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe', #r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', #r'C:\Program Files (x86)\Opera\launcher.exe', #r'C:\Users\Administrator\AppData\Roaming\qianying\qianying.exe', #r'C:\Users\Administrator\AppData\Local\115Chrome\Application\115chrome.exe', #r'C:\Program Files (x86)\Internet Explorer\iexplore.exe', #r'D:\Program Files(x86)\SouExplorer\SogouExplorer\SogouExplorer.exe' ] #选择一个浏览器 def chose_a_browser_open_url(browser_path,url): #如果传入的浏览器位置不存在,使用默认的浏览器打开 if not browser_path: print ('using default browser to open') web.open_new_tab(url)#使用默认浏览器,就不再结束进程 else: #判断浏览器路径是否存在 if not os.path.exists(browser_path): print ('current browser path not exists,using default browser') #浏览器位置不存在就使用默认的浏览器打开 browser_path='' chose_a_browser_open_url(chose_a_browser_open_url,url) else: browser_task_name=browser_path.split('\\')[-1]#结束任务的名字 browser_name=browser_task_name.split('.')[0]#自定义的浏览器代号 print (browser_name) web.register(browser_name, None,web.BackgroundBrowser(browser_path)) web.get(browser_name).open_new_tab(url)#使用新注册的浏览器打开网页 print ('using %s browser open url successful' % browser_name) time.sleep(5)#等待打开浏览器 # kill_cmd='taskkill /f /IM '+browser_task_name#拼接结束浏览器进程的命令 # os.system(kill_cmd) #终结浏览器 browser_path=random.choice(browser_paths)#随机从浏览器中选择一个路径 chose_a_browser_open_url(browser_path,url) if __name__ == '__main__': print (''''' ***************************************** ** Welcome to python of browser ** ** Created on 2019-04-28 ** ** @author: Louise_ZOU ** ***************************************** ''') open_url_use_random_browser()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值