我想我的输出数据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