我通過漫遊刮過朋友(結構化,如果笨重)網站的過程,以及將整個事物遷移到內容管理系統的長期目標,獲得了我的BeautifulSoup和python軸承。刮表
如果我拉出來只有一個細胞(soup = BeautifulSoup(urllib2.urlopen("http://www.bicyclepaintings.com/archive/index.html")
)在控制檯:
cell = soup.find_all('td',{'valign':'bottom'})[3]
我可以玩弄拉出子。這些都工作正常:cell.br.next_sibling
,cell.find('b').text
。但是,當我試圖遍歷所有的細胞,一個for循環:
def parse_archive(url):
soup = get_soup(url)
paintings = []
for cell in soup.find_all('td',{'valign':'bottom'}):
painting_title = cell.find('b').text
painting_media = cell.br.next_sibling
record = painting_title, painting_media
paintings.append(record)
return paintings
我得到一個屬性錯誤(AttributeError: 'NoneType' object has no attribute 'text'
)。我可以通過循環回到獲得一些相同的信息:
for item in cell.find_all('b'):
painting_title = item.text
但我不明白的方式來獲得在同級<br/>
和(更重要的一點),我不明白爲什麼它的工作原理如果我拉出一個項目,但如果我嘗試通過for循環訪問它們,則不會。我在這裏錯過了什麼?
作品。 'var = one.thing if thing else''語法是否會代替「如果標題是None:continue」? – Amanda
它不會工作,因爲標題需要是一個美麗的湯標籤。 –
我使用'if len(graph.contents)> 4:'在cell.find_all('font')中的圖的內部結束了''以排除空白單元格。 – Amanda