读取保存在文本文件中的源页面并提取文本
Reading source page saved in a text file and extracting text
我有多个文本文件,用于存储网站的源页面。所以每个文本文件都是一个源页面。
我需要使用以下代码从存储在文本文件中的 div class 中提取文本:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("zing.internet.accelerator.plus.txt"))
txt = soup.find('div' , attrs = { 'class' : 'id-app-orig-desc' }).text
print txt
我已经检查了我的汤对象的类型,以确保它在查找 div class 时没有使用字符串查找方法。
汤对象的类型
print type(soup)
<class 'bs4.BeautifulSoup'>
我已经参考了 the previous post 之一,并在 beautifulsoup 语句中写了 open 语句。
错误:
Traceback (most recent call last):
File "html_desc_cleaning.py", line 13, in <module>
txt2 = soup.find('div' , attrs = { 'class' : 'id-app-orig-desc' }).text
AttributeError: 'NoneType' object has no attribute 'text'
页面来源:
尝试替换为:
soup = BeautifulSoup(open("zing.internet.accelerator.plus.txt"))
有了这个:
soup = BeautifulSoup(open("zing.internet.accelerator.plus.txt").read())
顺便说一下,关闭文件是个好主意。您可以像这样使用 with
:
with open("zing.internet.accelerator.plus.txt") as f:
soup = BeautifulSoup(f.read())
with
将自动关闭文件。
这里有一个例子说明为什么你需要 .read()
函数:
>>> a = open('test.txt')
>>> type(a)
<class '_io.TextIOWrapper'>
>>> print(a)
<_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'>
>>> b = a.read()
>>> type(b)
<class 'str'>
>>> print(b)
Hey there.
>>> print(open('test.txt'))
<_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'>
>>> print(open('test.txt').read())
Hey there.
我已经解决了问题
在我的案例中,beautifulsoup 的默认解析器是 'lxml',它无法读取完整的源页面。
将解析器更改为 'html.parser' 对我有用。
f = open("zing.internet.accelerator.plus.txt")
soup = f.read()
bs = BeautifulSoup(soup,"html.parser")
print bs.find('div',{'class' : 'id-app-orig-desc'}).text
我有多个文本文件,用于存储网站的源页面。所以每个文本文件都是一个源页面。
我需要使用以下代码从存储在文本文件中的 div class 中提取文本:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("zing.internet.accelerator.plus.txt"))
txt = soup.find('div' , attrs = { 'class' : 'id-app-orig-desc' }).text
print txt
我已经检查了我的汤对象的类型,以确保它在查找 div class 时没有使用字符串查找方法。 汤对象的类型
print type(soup)
<class 'bs4.BeautifulSoup'>
我已经参考了 the previous post 之一,并在 beautifulsoup 语句中写了 open 语句。
错误:
Traceback (most recent call last):
File "html_desc_cleaning.py", line 13, in <module>
txt2 = soup.find('div' , attrs = { 'class' : 'id-app-orig-desc' }).text
AttributeError: 'NoneType' object has no attribute 'text'
页面来源:
尝试替换为:
soup = BeautifulSoup(open("zing.internet.accelerator.plus.txt"))
有了这个:
soup = BeautifulSoup(open("zing.internet.accelerator.plus.txt").read())
顺便说一下,关闭文件是个好主意。您可以像这样使用 with
:
with open("zing.internet.accelerator.plus.txt") as f:
soup = BeautifulSoup(f.read())
with
将自动关闭文件。
这里有一个例子说明为什么你需要 .read()
函数:
>>> a = open('test.txt')
>>> type(a)
<class '_io.TextIOWrapper'>
>>> print(a)
<_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'>
>>> b = a.read()
>>> type(b)
<class 'str'>
>>> print(b)
Hey there.
>>> print(open('test.txt'))
<_io.TextIOWrapper name='test.txt' mode='r' encoding='UTF-8'>
>>> print(open('test.txt').read())
Hey there.
我已经解决了问题
在我的案例中,beautifulsoup 的默认解析器是 'lxml',它无法读取完整的源页面。
将解析器更改为 'html.parser' 对我有用。
f = open("zing.internet.accelerator.plus.txt")
soup = f.read()
bs = BeautifulSoup(soup,"html.parser")
print bs.find('div',{'class' : 'id-app-orig-desc'}).text