python - 无法让我的脚本使用代理获取所需的内容
问题描述
我在 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)
解决方案
下面的代码对我来说效果很好,但是它不能帮助你处理不好的代理。它还遍历代理列表并尝试一个,直到成功或列表用完。它会打印它使用的代理,以便您可以看到它尝试了多次。
然而,正如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)
推荐阅读
- java - 从 ComboBox 中选择值时如何处理 for 循环的大小变化
- mysql - Laravel mysql if else 条件
- c - 为什么指向结构的指针的指针在以下代码中有效,但在单个指针中无效
- python - 来自未来的 Python3 / str 和 string_types 中等效的 Basestring,六个不起作用
- jquery - JQuery - 防止提交时刷新页面
- r - 从 JPEG 创建 Spark 对象并在非翻译函数上使用 spark_apply()
- node.js - 缓存静态资产的最佳算法
- angular - 方法中的 browser.wait 是否意味着该方法需要异步..?
- google-cloud-firestore - firestore.rules 正则表达式中的计算错误
- c - 一元“*”的无效类型参数(有“双”)