【发布时间】:2016-04-22 09:32:38
【问题描述】:
如何将美丽的汤输出数据保存到文本文件中?
这是代码;
import urllib2
from bs4 import BeautifulSoup
url = urllib2.urlopen("http://link").read()
soup = BeautifulSoup(url)
file = open("parseddata.txt", "wb")
for line in soup.find_all('a', attrs={'class': 'book-title-link'}):
print (line.get('href'))
file.write(line.get('href'))
file.flush()
file.close()
【问题讨论】:
-
file.flush()和file.close()应该在 for 循环之外。
标签: python python-2.7 file-io beautifulsoup