2017-06-19 53 views
-1

我剛剛開始使用Python。試圖從...刮表 http://www.dramexchange.com/如何在Python中刪除表格

但還沒有成功呢。

import requests, bs4 
r = requests.get('http://www.dramexchange.com/') 
soup = bs4.BeautifulSoup(r.text, 'lxml') 
for table in contents.find_all(class_='tb_NationalDramSpotPrice'): 
title = table.find(class_='title').text 
for tr in table.find_all('tr'): 
    _, Item, Daily High, Daily Low. Session High. Session Low. Session Average, Session Change = [td for td in tr.stripped_strings] 
    print(Item, Daily High, Daily Low. Session High. Session Low. Session Average, Session Change) 

任何幫助?

+0

你有什麼問題? –

回答

0

要啓動它看起來像你實際上並沒有在你的第一個迭代器引用正確的變量:

soup = bs4.BeautifulSoup(r.text, 'lxml') 
for table in contents.find_all(class_='tb_NationalDramSpotPrice'): 

通知你的變量contents調用find_all(),而不是soup如之前宣佈。

我走了額外的一步,真正拉入了你想要抓取的網站,它實際上並不包含'tb_NationalDramSpotPrice'類,而是包含該名稱的ID。這會增加額外的問題。