我想收集使用美麗的湯和Python的網址列表。我是兩個新用戶,我需要幫助瞭解如何使用通配符查找href值。 HTML代碼看起來像使用通配符查找href值Beautifulsoup
<table class="sortable stats_table" id="team_index">
<colgroup>...</colgroup>
<thead>...</thead>
<tbody>
<tr class data-row="0">
<td align="left">...</td>
<td align="left">...</td>
<td align="left">
<a href="/teams/crd/2013.htm">Arizona Cardinals</a>
</td>
<td align="right">6</td>
<tr class data-row="1">
<td align="left">...</td>
<td align="left">...</td>
<td align="left">
<a href="/teams/crd/2012.htm">Arizona Cardinals</a>
</td>
<td align="right">6</td>
</tbody>
<tfoot></tfoot>
</table>
爲了簡潔起見,我只包含了html表的前兩行。我想找到所有<a>
標籤與href="/teams/XXX/YYYY.htm"
其中XXX
是團隊名稱和YYYY
年,並把它們全部列入一個網址列表。現在我用下面的代碼
from bs4 import BeautifulSoup
from urllib2 import urlopen
import re
BASE_URL = "http://www.pro-football-reference.com"
teams_url = ("http://www.pro-football-reference.com/teams/crd/")
soup=BeautifulSoup(urlopen(teams_url),"lxml")
teamtable = soup.find(lambda tag: tag.name=="table" and tag.has_attr("id") and
tag["id"]=="team_index")
rows = teamtable.find_all("tr", attrs={""})
test=rows.find_all('a', {'href': lambda x : x.startswith('/teams/')})
masterlist = [BASE_URL + link.a["href"] for link in test]
其中變量teams_url
正在從先前編譯URL列表拉帶形式"http://www.pro-football-reference.com/teams/XXX/"
的成員。我提供的代碼提供了以下錯誤,由於最後一行:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'ResultSet' object has no attribute 'find_all'
soup.find_all('a', {'href': lambda x : x.startswith('/teams/')})
問題1)我怎樣才能使代碼收集所有的標籤在表格中我有一個像
masterlist = [`www.pro-football-reference.com/teams/crd/2013',
`www.pro-football-reference.com/teams/crd/2012',
`www.pro-football-reference.com/teams/crd/2011'
...]
列表
沒有對團隊縮寫進行硬編碼,因爲我將從每個團隊的列表中爲該代碼傳遞一個`teams_url'變量。
問題2)數據中有50年(行),但我只想從2012年到2000年,包括2000年。我該怎麼做?
而且,它可能看起來像有在我的代碼輸入錯誤,因爲這些行HTML代碼標記<tr class data-row="0">
,但是,由於某種原因,無論是LXML和HTML5分析器返回標籤的屬性作爲<tr class="">
,我不知道爲什麼或如何解決這個問題,所以任何額外的幫助都會很棒。
謝謝
感謝您的幫助。我打算將整個過程嵌套在一個循環中並遍歷團隊。如果我想離開團隊目錄文件夾通用,那麼在每次迭代時它只會找到所有具有屬性'href =「/ teams/xxx/yyyy.htm」''的'標籤,此外,只有2000 - 2012年? –
然後只需更改正則表達式。 – VooDooNOFX