想从我的html文件中提取文本。如果我使用下面的特定文件:使用bs4提取html文件中的文本
import bs4, sys
from urllib import urlopen
#filin = open(sys.argv[1], 'r')
filin = '/home/iykeln/Desktop/R_work/file1.html'
webpage = urlopen(filin).read().decode('utf-8')
soup = bs4.BeautifulSoup(webpage)
for node in soup.findAll('html'):
print u''.join(node.findAll(text=True)).encode('utf-8')
它会工作。 但是使用开放下面试图对非特定文件(sys.argv中[1], 'R'):
import bs4, sys
from urllib import urlopen
filin = open(sys.argv[1], 'r')
#filin = '/home/iykeln/Desktop/R_work/file1.html'
webpage = urlopen(filin).read().decode('utf-8')
soup = bs4.BeautifulSoup(webpage)
for node in soup.findAll('html'):
print u''.join(node.findAll(text&