PHANTOMJS Setting up Agents

Source: Internet
Author: User

PHANTOMJS can be configured with the proxy IP

#coding =utf-8
Import OS
Import re
Import time
Import requests
From Scrapy.selector import Htmlxpathselector
From scrapy.http import Htmlresponse
From selenium import Webdriver
From Selenium.webdriver.common.proxy import Proxytype

Import Sys
Reload (SYS)
Sys.setdefaultencoding ("Utf-8")

Import warnings
Warnings.filterwarnings ("Ignore")


if __name__ = = ' __main__ ':
Path_phantomjs=r ' D:\phantomjs\bin\phantomjs.exe '
Browser=webdriver. PHANTOMJS (PATH_PHANTOMJS)
#真实ip
Browser.get (' http://icanhazip.com/') #获取ip网址
Response = Htmlresponse (url= ", Body=str (Browser.page_source))
HXS = Htmlxpathselector (response)
print ' Your IP is: ', '. Join (Hxs.select ('//text () '). Extract ()). Strip ()
#代理ip
Proxy=webdriver. Proxy ()
Proxy.proxy_type=proxytype.manual
Proxy.http_proxy= ' 220.248.229.45:3128 '
#将代理设置添加到webdriver. In Desiredcapabilities.phantomjs
Proxy.add_to_capabilities (Webdriver. DESIREDCAPABILITIES.PHANTOMJS)
Browser.start_session (Webdriver. DESIREDCAPABILITIES.PHANTOMJS)
Browser.get (' http://icanhazip.com/') #获取ip网址
Response = Htmlresponse (url= ", Body=str (Browser.page_source))
HXS = Htmlxpathselector (response)
print ' Your proxy IP is: ', '. Join (Hxs.select ('//text () '). Extract ()). Strip ()

Tested, actually effective, as follows:

Later, you can consider applying this method to the download to reduce the chance of spiders being blocked.

PHANTOMJS Setting up Agents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.