如何将我的 Beautiful soup 输出数据保存到文本文件中?
这是代码;
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.close
应该调用一次(在 for
循环之后):
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'}):
href = line.get('href')
print href
if href:
file.write(href + '\n')
file.close()
更新 您可以使用href=True
来避免if
语句。除此之外,使用 with
statement ,您不需要手动关闭文件对象:
import urllib2
from bs4 import BeautifulSoup
content = urllib2.urlopen("http://link").read()
soup = BeautifulSoup(content)
with open('parseddata.txt', 'wb') as f:
for a in soup.find_all('a', attrs={'class': 'book-title-link'}, href=True):
print a['href']
f.write(a['href'] + '\n')
关于python - 如何将 Beautiful soup 输出数据保存到文本文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34833951/