python - 让 selenium 在 pythonanywhere 上工作

标签 python selenium selenium-webdriver python-3.5 pythonanywhere

我的理解是 pythonanywhere 支持 headless Firefox 浏览器,但你需要

from pyvirtualdisplay import Display

因此您可以使用

with Display():
    while True:
        try:
            driver = webdriver.Firefox()
            break
        except:
            time.sleep(3)

我连接得很好。但是,在我开始使用驱动程序之后

with Display():
    while True:
        try:
            driver = webdriver.Firefox()
            break
        except:
            time.sleep(3)
    wb=load_workbook(r'/home/hoozits728/mutual_fund_tracker/Mutual_Fund_Tracker.xlsx')
    ws=wb.get_sheet_by_name('Tactical')

    for i in range(3, ws.max_row+1):
        if ws.cell(row=i,column=2).value is not None:
            driver.get('https://finance.yahoo.com/quote/' + ws.cell(row=i,column=2).value + '/performance?ltr=1')
            oneyear=driver.find_element_by_css_selector('#Col1-0-Performance-Proxy > section > div:nth-child(2) > div > div:nth-child(5) > span:nth-child(2)').text
            threeyear=driver.find_element_by_css_selector('#Col1-0-Performance-Proxy > section > div:nth-of-type(2) > div > div:nth-of-type(6) > span:nth-of-type(2)').text
            fiveyear=driver.find_element_by_css_selector('#Col1-0-Performance-Proxy > section > div:nth-of-type(2) > div > div:nth-of-type(7) > span:nth-of-type(2)').text
            ws.cell(row=i,column=10).value=oneyear
            ws.cell(row=i,column=11).value=threeyear
            ws.cell(row=i,column=12).value=fiveyear

           … and so on …

过了一会儿我得到了这个错误

enter image description here

就其值(value)而言,这段代码在我的本地机器上运行得非常好。另外,我是付费成员(member),所以不应该有白名单问题。

最佳答案

我最近了解到,yahoo 已阻止 pythonanywhere 运行任何网络抓取脚本。我假设这对所有 AWS 服务器和使用它们的人都是如此,但我不是 100% 确定这一点。我希望这对遇到这个问题的任何人都有帮助。

https://www.pythonanywhere.com/forums/topic/5724/#id_post_52307

关于python - 让 selenium 在 pythonanywhere 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52353597/

相关文章:

python - 在 Python 的不同子图中绘制矩形

java - 如何使用 WebDriver 和 Java 制作 "background-image"CSS 列表

python - 如何使用 Selenium 和 Python 与 reCAPTCHA 音频元素交互

java - 如何使用 Selenium 和 Java 通过 sendKeys 方法传递双引号字符串

python - 使用一个 PDE 的解来定义另一个 PDE - FEniCS

python - 查询在 SQL Server 中工作但在 sqlite 中不起作用

javascript - Selenium:如何编写同步 Selenium 自动化?

python - 如何使用 Selenium 和 Python 更改用户代理

java - WebElement 中的 sendKeys (java.lang.CharSequence...) 无法应用于 (org.openqa.selenium.Keys)

python - 在python中解码十六进制utf8字符串