Skip to main content
 首页 » 编程设计

python之如何将 Beautiful soup 输出数据保存到文本文件中

2024年10月24日14yyy_WW

如何将我的 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')