【问题标题】:Getting selenium to work on pythonanywhere让 selenium 在 pythonanywhere 上工作
【发布时间】:2019-02-20 13:02:33
【问题描述】:

我的理解是pythonanywhere支持无头火狐浏览器但你需要

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 …

过了一会儿,我收到了这个错误

不管怎样,这段代码在我的本地机器上运行良好。另外,我是付费会员,所以应该没有白名单问题。

【问题讨论】:

  • 使用错误堆栈跟踪更新问题
  • @New 贡献者 你能看到图片吗?我无法从 pythonanywhere 控制台一次复制每一行。
  • 听起来浏览器崩溃了;也许这与页面内容有关?如果你点击不同的 URL,比如https://www.google.com/,会发生什么?
  • @GilesThomas 你搞定了!显然我的网址有问题;但是,直接在浏览器中输入 url 会产生一个正确的网页。这是代码解析为:finance.yahoo.com/quote/USCBX/performance?ltr=1
  • 我正要回复,但我看到你已经在下面的答案中找到了问题——已经投票了:-)

标签: python selenium selenium-webdriver python-3.5 pythonanywhere


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    您收到该错误是因为 selenium 无法连接到您创建的浏览器。如果您正在运行第一块代码,然后是第二块代码,则显示已关闭,这可能会导致浏览器崩溃。

    您需要在 inside with 块中运行使用浏览器的代码。

    PythonAnywhere 帮助页面上有一个示例,展示了如何以最可靠的方式完成所有这些操作。

    【讨论】:

    • 但它都在with Display():内部运行。
    • 我已经更新了我的问题,以使这一点更清楚。非常感谢您的回复。
    猜你喜欢
    • 2013-08-05
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2019-07-30
    • 2021-02-22
    • 2018-05-22
    相关资源
    最近更新 更多