首页 > 解决方案 > 无法让我的脚本使用代理获取所需的内容

问题描述

我在 python 中结合 selenium 编写了一个脚本,使用代理来获取导航到 url 时填充的不同链接的文本,就像在这个. 我想从那里解析的是连接到每个链接的可见文本。

到目前为止,我尝试过的脚本能够start_script()在其中调用此函数时生成新的代理。问题是这个 url 把我带到了这个重定向的链接。只有当我继续尝试直到 url 对代理感到满意时,我才能摆脱这种重定向。我当前的脚本只能使用两个新代理尝试两次。

如何get_texts()以这种方式在函数中使用任何循环,以便它会继续尝试使用新的代理,直到它解析所需的内容?

到目前为止我的尝试:

import requests
import random
from itertools import cycle
from bs4 import BeautifulSoup
from selenium import webdriver

link = 'http://www.google.com/search?q=python'

def get_proxies():   
    response = requests.get('https://www.us-proxy.org/')
    soup = BeautifulSoup(response.text,"lxml")
    proxies = [':'.join([item.select_one("td").text,item.select_one("td:nth-of-type(2)").text]) for item in soup.select("table.table tbody tr") if "yes" in item.text]
    return proxies

def start_script():
    proxies = get_proxies()
    random.shuffle(proxies)
    proxy = next(cycle(proxies))
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument(f'--proxy-server={proxy}')
    driver = webdriver.Chrome(chrome_options=chrome_options)
    return driver

def get_texts(url):
    driver = start_script()
    driver.get(url)
    if "index?continue" not in driver.current_url:
        for item in [items.text for items in driver.find_elements_by_tag_name("h3")]:
            print(item)
    else:
        get_texts(url)

if __name__ == '__main__':
    get_texts(link)

标签: pythonpython-3.xseleniumselenium-webdriverweb-scraping

解决方案


下面的代码对我来说效果很好,但是它不能帮助你处理不好的代理。它还遍历代理列表并尝试一个,直到成功或列表用完。它会打印它使用的代理,以便您可以看到它尝试了多次。

然而,正如https://www.us-proxy.org/指出的那样:

什么是谷歌代理?支持在 Google 上搜索的代理称为 Google 代理。有些程序需要它们在 Google 上进行大量查询。自 2016 年以来,所有的谷歌代理都死了。阅读那篇文章以获取更多信息。

文章:

谷歌在 2016 年阻止代理 如果检测到代理,谷歌会显示一个页面来验证您是人类而不是机器人。在 2016 年之前,如果您可以通过此人工验证,Google 允许在一段时间内使用该代理。

from contextlib import contextmanager
import random

from bs4 import BeautifulSoup
import requests
from selenium import webdriver


def get_proxies():   
    response = requests.get('https://www.us-proxy.org/')
    soup = BeautifulSoup(response.text,"lxml")
    proxies = [':'.join([item.select_one("td").text,item.select_one("td:nth-of-type(2)").text]) for item in soup.select("table.table tbody tr") if "yes" in item.text]
    random.shuffle(proxies)
    return proxies


# Only need to fetch the proxies once
PROXIES = get_proxies()


@contextmanager
def proxy_driver():
    try:
        proxy = PROXIES.pop()
        print(f'Running with proxy {proxy}')
        chrome_options = webdriver.ChromeOptions()
        # chrome_options.add_argument("--headless")
        chrome_options.add_argument(f'--proxy-server={proxy}')
        driver = webdriver.Chrome(options=chrome_options)
        yield driver
    finally:
        driver.close()

def get_texts(url):
    with proxy_driver() as driver:
        driver.get(url)
        if "index?continue" not in driver.current_url:
            return [items.text for items in driver.find_elements_by_tag_name("h3")]
        print('recaptcha')

if __name__ == '__main__':
    link = 'http://www.google.com/search?q=python'
    while True:
        links = get_texts(link)
        if links:
            break
    print(links)

推荐阅读