Python 从HTML数据中提取文本
我在从凌乱的HTML数据中提取信息时遇到问题。基本上,我想做的是从给定的HTML代码片段中只提取实际显示的单词。下面是我得到的原始HTML数据的一个示例Python 从HTML数据中提取文本,python,html,beautifulsoup,text-extraction,Python,Html,Beautifulsoup,Text Extraction,我在从凌乱的HTML数据中提取信息时遇到问题。基本上,我想做的是从给定的HTML代码片段中只提取实际显示的单词。下面是我得到的原始HTML数据的一个示例 <p>I have an app which send mail to my defined mail address "myemail@own.com". For this i create my own Custom Email View Which contains check boxes message body and o
<p>I have an app which send mail to my defined mail address "myemail@own.com". For this i create my own Custom Email View Which contains check boxes message body and other options. Now i want that when send button is pressed my app should not go to gmail view or other email client view it directly submit the data</p>
<p>String recepientEmail = "myemail@own.comm"; </p>
<p>// either set to destination email or leave empty</p>
<pre><code> Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + recepientEmail));
startActivity(intent);
</code></pre>
<p>but on submit it opens gmail or chooser email client view but i dont want to show gmail view</p>
但在提交时,它会打开gmail或chooser电子邮件客户端视图,但我不想显示gmail视图
我想把它变成这个
<span id="midArticle_1"></span><p>Here is the First Paragraph.</p><span id="midArticle_2"></span><p>Here is the second Paragraph.</p><span id="midArticle_3"></span><p>Paragraph Three."</p>
print html.parse(url).xpath('//p/text()')
因此,基本上只需检索每个
标记中的所有内容并将它们连接在一起。我正在使用python,所以我认为BeautifulSoup可能是最好的方法,但是我似乎不知道如何做到这一点。我还想在几个这样的示例(实际上是数百万个)上重复这一点,但每个示例都应该至少有一个标记。是一个Python脚本,它将HTML页面转换为干净、易于阅读的纯ASCII文本。更好的是,ASCII恰好也是有效的标记(文本到HTML格式)
这是第一段。这是第二段。第三段。”
打印html.parse(url.xpath('//p/text()'))
输出
[“这是第一段。”,“这是第二段。”,
“第三款。”]
是一个Python脚本,它将HTML页面转换为干净、易于阅读的纯ASCII文本。更好的是,ASCII恰好也是有效的标记(文本到HTML格式)
这是第一段。这是第二段。第三段。”
打印html.parse(url.xpath('//p/text()'))
输出
[“这是第一段。”,“这是第二段。”,
“第三款。”]
使用BeautifulSoup
模块从
标记中提取所有文本的一种方法
script.py的内容
:
python3 script.py infile
像这样运行:
I have an app which send mail to my defined mail address "myemail@own.com". For this i create my own Custom Email View Which contains check boxes message body and other options. Now i want that when send button is pressed my app should not go to gmail view or other email client view it directly submit the data String recepientEmail = "myemail@own.comm"; // either set to destination email or leave empty but on submit it opens gmail or chooser email client view but i dont want to show gmail view
这将产生:
import requests
from bs4 import BeautifulSoup
r = requests.get("your url")
html_text = r.text
soup = BeautifulSoup(html_text)
clean_html = ''.join(soup.findAll(text=True))
print(clean_html)
使用BeautifulSoup
模块从
标记中提取所有文本的一种方法
script.py的内容
:
python3 script.py infile
像这样运行:
I have an app which send mail to my defined mail address "myemail@own.com". For this i create my own Custom Email View Which contains check boxes message body and other options. Now i want that when send button is pressed my app should not go to gmail view or other email client view it directly submit the data String recepientEmail = "myemail@own.comm"; // either set to destination email or leave empty but on submit it opens gmail or chooser email client view but i dont want to show gmail view
这将产生:
import requests
from bs4 import BeautifulSoup
r = requests.get("your url")
html_text = r.text
soup = BeautifulSoup(html_text)
clean_html = ''.join(soup.findAll(text=True))
print(clean_html)
我最近开始玩漂亮的汤。
我发现这行代码非常有用。我会把我的整个例子都放进去给你们看
希望这对你有用/回答了你的问题我最近开始玩漂亮的汤。
我发现这行代码非常有用。我会把我的整个例子都放进去给你们看
希望这对你有用/回答你的问题酷发现!所以这会删除所有标签中的所有内容,除了标签?我希望输出只是一个大字符串。所以我想我可以加入您提供的输出。对不起,我不确定“html”对象是什么。你在这个例子中使用html2text吗?很酷!所以这会删除所有标签中的所有内容,除了标签?我希望输出只是一个大字符串。所以我想我可以加入您提供的输出。对不起,我不确定“html”对象是什么。您在本例中使用的是html2text吗?谢谢。谁能告诉我这两种解决方案中哪一种最快?我有很多例子要讲。谢谢很抱歉对于一些示例,我得到连接行的错误“sequence item 1:expected string或Unicode,NoneType found”。你能告诉我怎么避开这个吗?谢谢。谁能告诉我这两种解决方案中哪一种最快?我有很多例子要讲。谢谢很抱歉对于一些示例,我得到连接行的错误“sequence item 1:expected string或Unicode,NoneType found”。你能告诉我怎么避开这个吗?