python - Python获取Html页面内容的方法

标签 python html parsing

我已将网页下载到 html 文件中。我想知道获取该页面内容的最简单方法是什么。关于内容,我的意思是我需要浏览器会显示的字符串。

要清楚:

输入:

<html><head><title>Page title</title></head>
       <body><p id="firstpara" align="center">This is paragraph <b>one</b>.
       <p id="secondpara" align="blah">This is paragraph <b>two</b>.
       </html>

输出:

Page title This is paragraph one. This is paragraph two.

放在一起:

from BeautifulSoup import BeautifulSoup
import re

def removeHtmlTags(page):
    p = re.compile(r'''<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>''')
    return p.sub('', page)

def removeHtmlTags2(page):
    soup = BeautifulSoup(page)
    return ''.join(soup.findAll(text=True))

相关

最佳答案

Beautiful Soup 解析 HTML .

要获取没有标签的所有文本,请尝试:

''.join(soup.findAll(text=True))

关于python - Python获取Html页面内容的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2416823/

相关文章:

python - Sikuli RNG 脚本重复相同的数字

python - 使用 python 查找视频中的图像

html - 使用 Flying Saucer 以 PDF 格式打印图像

java - 使用年份和不同的亚秒位数解析 LocalDateTime

android - android编辑文本数组的解析方法

python - 如何延迟 Python 中脚本的执行?

python - 无法理解 pyOpenGl 顶点和边

javascript - 从表单外部的字段发送数据

html - 在 CSS 中将提交按钮作为表单的扩展

java - 将逻辑文本解析为Json结构