def proxyRquest_normal(url, data, headers): targeturl = "https://www.weiyun.com" flag = True while flag: try: # begTime = time.time() proxy_addr = ipPool.randomGetIp(targeturl) # endTime = time.time() # print("代理IP获取时间:%s"%(str(endTime-begTime))) proxies = { "http": "http://" + proxy_addr, "https": "http://" + proxy_addr, } flag = False # begTime = time.time() result = requests.request('POST', url, proxies=proxies, data=json.dumps(data), headers=headers, verify=False, timeout=5) # endTime = time.time() # print("代理请求时间:%s"%(str(endTime-begTime))) except requests.exceptions.ProxyError as err: flag = True except WinError as err: print(err) flag = True return result
def proxyRequest(url,header,data): targeturl="http://www.xiaofamao.com/" flag = True while flag: try : flag=False proxy_addr=ipPool.randomGetIp(targeturl) proxy = urllib.request.ProxyHandler({'http': proxy_addr,'https': proxy_addr}) opener = urllib.request.build_opener(proxy, urllib.request.ProxyHandler) urllib.request.install_opener(opener) data = urllib.parse.urlencode(data).encode('utf-8') req = urllib.request.Request(url, data=data, headers=header,method = 'POST') response = urllib.request.urlopen(req) html=response.read().decode('UTF-8') except urllib.error.URLError as err: print(err) flag=True return html
def proxyRquest_file(url, data, files, headers): targeturl = "https://www.weiyun.com" flag = True while flag: try: proxy_addr = ipPool.randomGetIp(targeturl) proxies = { "http": "http://" + proxy_addr, "https": "http://" + proxy_addr, } flag = False result = requests.request('POST', url, proxies=proxies, data=data, files=files, headers=headers, verify=False) except requests.exceptions.ProxyError as err: flag = True return result
def proxyRequest(url, header): targeturl = "http://weixin.sogou.com" flag = True while flag: try: flag = False proxy_addr = ipPool.randomGetIp(targeturl) proxy = urllib.request.ProxyHandler({ 'http': proxy_addr, 'https': proxy_addr }) opener = urllib.request.build_opener(proxy, urllib.request.ProxyHandler) urllib.request.install_opener(opener) req = urllib.request.Request(url, None, headers=header) response = urllib.request.urlopen(req) html = response.read().decode('UTF-8') except urllib.error.URLError as err: print(err) flag = True return html
# "Origin": "http://www.xiaofamao.com", # "Referer": "http://www.xiaofamao.com/", # "Upgrade-Insecure-Requests": "1", # "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36" # } # url="http://www.xiaofamao.com/result.php" # data={ # "contents":"自信与自卑是两个极端,一般来说,我更愿意相信自卑之人,自卑之人知道自己的弱点,在做事情上会最大限度的规避错误,避免风险。而自信之人总是容易陷入盲目自信以及过度自信中去。极度自信者大多浅薄,你仔细推...", # "xfm_uid":"0443e6a06583e46457ee2fc3a484ce14", # "agreement":"on" # } # html=proxyRequest(url,header,data) # print(html) chrome_options = Options() # chrome_options.add_argument("--headless") chrome_options.add_argument("--no-sandbox") chrome_options.add_argument('lang=zh_CN.utf-8') targeturl="http://www.xiaofamao.com/" proxy_addr=ipPool.randomGetIp(targeturl) print(proxy_addr) chrome_options.add_argument("--proxy-server=http://"+proxy_addr) # chrome_options.add_argument("user-data-dir=C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\User Data"); # chrome_options.add_extension("C:\\Users\\Administrator\\Downloads\\Set-Character-Encoding_v0.42.crx"); # 对应的chromedriver的放置目录 driver = webdriver.Chrome(executable_path=(_env["chrome_driver_path"]), chrome_options=chrome_options) driver.get("http://www.xiaofamao.com/")