值我是新來的Python和我正在寫一個webscraper,以查找<td>
行的HTML表:檢查是否存在
# open CSV with URLS to scrape
csv_file = csv.reader(open('urls.csv', 'rb'), delimiter=',')
names = []
for data in csv_file:
names.append(data[0])
for name in names:
html = D.get(name);
html2 = html
param = '<br />';
html2 = html2.replace("<br />", " | ")
print name
c = csv.writer(open("darkgrey.csv", "a"))
for row in xpath.search(html2, '//table/tr[@class="bgdarkgrey"]'):
cols = xpath.search(row, '/td')
c.writerow([cols[0], cols[1], cols[2], cols[3], cols[4]])
它所做的是從4表中獲取價值'<td>'
問題是,有些桌子沒有cols[2]
,cols[3]
或cols[4]
有沒有辦法,我可以檢查這些是否存在?
感謝
有點偏離主題,但是你真的想追加到「darkgrey.csv」嗎?如果我在哪裏,我會在全局範圍內用「w」打開該文件,以防止在您再次測試該腳本時它增長到inf。還請確保關閉它! – RickyA