让selenium在pythonanywhere上工作

问题描述 投票:7回答:2

我的理解是pythonanywhere支持无头的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

对于它的价值,这段代码在我的本地机器上运行得非常好。此外,我是付费会员,所以不应该有白名单问题。

python selenium selenium-webdriver python-3.5 pythonanywhere
2个回答
5
投票

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

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


0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.