2015-08-15 85 views
0

我訪問以下網站提取的個股名單:提取從網站使用BeautifulSoup(Python)的具體信息

http://www.barchart.com/stocks/performance/12month.php

我使用下面的代碼:

from bs4 import BeautifulSoup 
import requests 

url=raw_input("http://www.barchart.com/stocks/performance/12month.php") 
r = requests.get("http://www.barchart.com/stocks/performance/12month.php") 
data = r.text 
soup =BeautifulSoup(data, "lxml") 
for link in soup.find_all('a'): 
    print(link.get('href')) 

問題是我收到很多不需要的其他信息。我想問問什麼是一種只會給我股票名稱而沒有別的辦法。

+0

你在問a)如果有一個? b)如果我們應該爲你寫一個嗎? - 情況a)最有可能。案例b)否。請縮小問題的範圍。 – Emz

+0

我在問是否有辦法。由於我不知道如何直接進行,因此我將該文件複製到文本文件中,並從中提取關鍵字。我不知道這是否是這樣做的最快方法... – Raptor776

回答

0
r = requests.get("http://www.barchart.com/stocks/performance/12month.php") 
html = r.text 
soup = BeautifulSoup(html, 'html.parser') 
tds = soup.find_all("td", {"class": "ds_name"}) 
for td in tds: 
    print td.a.text 

如果你看看頁面的源代碼,你會發現你需要的是在一個表中。具體而言,股票的名稱是<td></td>,其class="ds_name"。就是這樣了。

+0

非常感謝!不知道soup.find_all – Raptor776