我的理解是 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 …
过了一会儿我得到了这个错误
就其值(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/