我想我的輸出數據beautifulsoup與2列的CSV:1.標題,背景輸出beautifulsoup數據到CSV
所以標題欄應該有soup.Title然後描述應該是print語句是在與開始在courselinks X環......
**#This is what I tried:**
with open('newcsv.csv','wb') as f:
writer = csv.writer(f, delimiter='\t')
writer.writerow('Title')
for x in courselinks[0:3]:
data = requests.get(("http:"+x)
soup = bs(data.text)
print soup.title #This I want in the Title column
for header in soup.find_all(text='Description'):
nextNode = header.parent
while True:
nextNode = nextNode.nextSibling
if nextNode is None:
break
if isinstance(nextNode, Tag):
print (nextNode.get_text(strip=True).strip().encode('utf-8')) **#This I want in the Description column**
if isinstance(nextNode, NavigableString):
print (nextNode.strip().encode('utf-8')) **#This I want in the Description column**
if isinstance(nextNode, Tag):
if nextNode.name == "h2":
break
難道你不想要以'courselink [0:3]:'爲縮進的''開頭的行嗎? –
對不起格式問題,他們縮進我的原始 – user6754289
是格式化問題,他們在我的原始代碼。我只是想讓兩個打印語句寫入同一個單元格。 – user6754289