2016-05-15 93 views
-3

好吧,我已經構建了一個計劃來抓取雅虎財務。我想要某個股票的歷史價格。然後我希望它被寫入Excel電子表格。它盡其所能,但它給了我整個頁面上的所有數據!我只需要表格中的數據。謝謝。如何更有效地刮這張桌子?

import urllib 
import urllib.request 
from bs4 import BeautifulSoup 
import os 
import requests 

def make_soup(url): 
    thepage = urllib.request.urlopen(url) 
    soupdata = BeautifulSoup(thepage, "html.parser") 
    return soupdata 

playerdatasaved="" 
soup = make_soup("https://finance.yahoo.com/q/hp?s=USO+Historical+Prices") 
for record in soup.findAll('tr'): 
playerdata="" 
for data in record.findAll('td'): 
    playerdata=playerdata+","+data.text 
if len(playerdata)!=0: 
    playerdatasaved = playerdatasaved + "\n" + playerdata[1:] 

header="Open,Close,High,Low" 
file = open(os.path.expanduser("Uso.csv"),"wb") 
file.write(bytes(header, encoding="ascii",errors='ignore')) 
file.write(bytes(playerdatasaved, encoding="ascii",errors='ignore')) 

print(playerdatasaved) 
+2

你沒問這個問題[這裏](HTTP ://stackoverflow.com/questions/37240961/how-do-i-scrape-just-the-table)幾個小時前在另一個帳戶? Stack Overflow並沒有意識到使多個賬戶重複提出同樣的問題。 – miradulo

+0

如果你在問題中搞砸了某些東西,你可以編輯它(在問題底部有一個按鈕來做到這一點) – amiller27

+0

我做了,但沒有回答,我真的需要它回答。我知道每個人都是編碼上帝,並且很樂意降到我的水平,但我真的沒有時間去處理這一切......我只需要這個想法。回答下一個人,我認爲messup是鏈接。嘗試使用此鏈接: – Jake

回答

0

獲取數據的表:

soup = make_soup("https://finance.yahoo.com/q/hp?s=USO+Historical+Prices") 
table = [[cell.text for row in soup.findAll('tr')] for cell in soup.findAll('td')] 

爲了數據的表寫入到一個文件:

import csv 

with open("output.csv", "wb") as f: 
    writer = csv.writer(f) 
    writer.writerows(table)