我正在用selenium抓取一个电子商务网站,因为这些页面是由Javascipt加载的。
这是工作流程 - : 1.在发送随机用户代理的同时,以虚拟显示模式实例化web潜水员驱动程序。使用随机用户代理可以降低您的检测机会。这不会减少IP阻塞的可能性。 2.对于每个查询字词,请说“睡衣” - 为该网站创建搜索网址 - 然后打开网址。 3.从Xpath获取相应的文本元素,比如十大产品,价格,产品名称等。 4.将它们存储在一个文件中 - 我将进一步处理
我有超过38000个这样的网址,我需要在页面加载时获取元素。 我做了多处理,我很快意识到这个过程失败了一段时间后,网站被阻止了,所以页面加载没有发生。
我如何在Python中进行IP欺骗,并且它是否适用于为您驱动网络的selenium,而不是urllib / urlopen?
除了通过xpath设置实际提取之外,这里是基本代码 - 更具体地说,请参阅init_driver
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import argparse
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import codecs, urllib, os
import multiprocessing as mp
from my_custom_path import scraping_conf_updated as sf
from fake_useragent import UserAgent
def set_cookies(COOKIES, exp, driver):
for key, val in COOKIES[exp].items():
driver.add_cookie({'name': key, 'value': val, 'path': '/', 'secure': False, 'expiry': None})
return driver
def check_cookies(driver, exp):
print "printing cookie name & value"
for cookie in driver.get_cookies():
if cookie['name'] in COOKIES[exp].keys():
print cookie['name'], "-->", cookie['value']
def wait_for(driver):
if conf_key['WAIT_FOR_ID'] != '':
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, conf_key['WAIT_FOR_ID'])))
elif conf_key['WAIT_FOR_CLASS'] != '':
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS, conf_key['WAIT_FOR_CLASS'])))
return driver
def init_driver(base_url, url, exp):
display = Display(visible=0, size=(1024, 768))
display.start()
profile = webdriver.FirefoxProfile()
ua = UserAgent(cache=False)
profile.set_preference("general.useragent.override",ua.random)
driver=webdriver.Firefox(profile)
if len(conf_key['COOKIES'][exp]) != 0:
driver.get(base_url)
driver.delete_all_cookies()
driver = set_cookies(COOKIES, exp, driver)
check_cookies(driver, exp)
driver.get(url)
driver.set_page_load_timeout(300)
if len(conf_key['POP_UP']['XPATH']) > 0:
driver = identify_and_close_popup(driver)
driver = wait_for(driver)
return driver
答案 0 :(得分:-1)
使用VPN提供商或http或短袜代理来更改您目标网站上的明显IP地址