安装:
- 安装selenium:
pip install selenium
- 安装phantomjs: 下载包,将bin/phantomjs.exe文件加入到环境变量path中即可
- 安装selenium:
请求头设置:
dcap = dict(DesiredCapabilities.PHANTOMJS)
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Cache-Control': 'max-age=0',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
'Connection': 'keep-alive',
'Host': 'club.autohome.com.cn',
\# 'Upgrade - Insecure - Requests':1,
'Referer': 'https://www.autohome.com.cn/shanghai/',
}
for key, value in headers.iteritems():
dcap['phantomjs.page.customHeaders.{}'.format(key)] = value
driver = webdriver.PhantomJS(desired_capabilities=dcap)
添加代理:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
def get_cookie(url,source='xcar'):
""" 爱卡出现安全页面,模拟输入验证码获得cookie """
cookies={}
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.loadImages"] = False
check_ip_list(source=source)
proxy = ipObjectsToList(source)
proxy_random = random.choice(proxy)
service_args = [
'--proxy={}'.format(proxy_random), #'http://192.168.1.2:80'
'--proxy-type=http',
]
driver = webdriver.PhantomJS(executable_path=phantomjs_path, desired_capabilities=dcap, service_args=service_args)
\#url='http://www.xcar.com.cn/favicon.ico'
driver.get(url)
driver.refresh()
cookies_temp=driver.get_cookies()
cookies_temp={ x['name']:x['value'] for x in cookies_temp }
cookies.update(cookies_temp)
driver.close()
return cookies