代码之家  ›  专栏  ›  技术社区  ›  Matt Cremeens

让硒在蟒蛇身上工作

  •  7
  • Matt Cremeens  · 技术社区  · 6 年前

    我的理解是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

    2 回复  |  直到 6 年前
        1
  •  5
  •   Matt Cremeens    6 年前

    据我最近了解,雅虎已经阻止pythonanywhere运行任何网页抓取脚本。我假设所有的AWS服务器和使用它们的人都是这样,但我不是100%确定。我希望这能帮助任何遇到这个问题的人。

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

        2
  •  0
  •   Glenn    6 年前

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

    这辆车有挡块。

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