gpt4 book ai didi

python - 使用 Selenium 进行 IP 欺骗/购买共享代理?

转载 作者:行者123 更新时间:2023-12-05 08:10:04 25 4
gpt4 key购买 nike

我正在使用 selenium 抓取一个电子商务网站,因为这些页面是由 Javascipt 加载的。

这是工作流程-:1. 在虚拟显示模式下实例化一个 web 驱动程序,同时发送一个随机用户代理。使用随机用户代理会稍微降低您被检测到的机会。这不会减少被 IP 阻止的机会。2. 对于每个查询词,说“睡衣”——为该网站创建搜索 url——并打开该 url。3. 从 Xpath 中获取相应的文本元素,比如前 10 名的产品 ID、它们的价格、产品名称等。4. 将它们存储在文件中 - 我将进一步处理

我有超过 38000 个这样的 url 需要在页面加载时获取元素。我做了多处理,我很快意识到这个过程失败了,因为过了一会儿,网站被阻止了,所以页面加载没有发生。

我如何在 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

最佳答案

使用 vpn 提供商或 http 或 socks 代理更改目标网站的明显原始 IP 地址

关于python - 使用 Selenium 进行 IP 欺骗/购买共享代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28396326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com