Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中使用lxml从html中提取除css和javascript之外的所有文本?_Python_Xpath_Python Requests_Lxml - Fatal编程技术网

如何在Python中使用lxml从html中提取除css和javascript之外的所有文本?

如何在Python中使用lxml从html中提取除css和javascript之外的所有文本?,python,xpath,python-requests,lxml,Python,Xpath,Python Requests,Lxml,如何从html中提取除css和javascript之外的所有文本 我正在尝试以下代码: r = requests.get(website) tree = html.fromstring(r.text) html_text = tree.xpath('//text()') 但它也从网站检索所有css和javascript内容您可以使用该方法删除您不感兴趣的元素 tree = html.fromstring(r.text) unwanted = tree.xpath('//script|//st

如何从html中提取除css和javascript之外的所有文本

我正在尝试以下代码:

r = requests.get(website)
tree = html.fromstring(r.text)
html_text = tree.xpath('//text()')
但它也从网站检索所有css和javascript内容

您可以使用该方法删除您不感兴趣的元素

tree = html.fromstring(r.text)

unwanted = tree.xpath('//script|//style')
for u in unwanted:
    u.drop_tree()

html_text = tree.xpath('//text()') 

那么你想排除和标记中的所有内容?@mzjn是的,没错。我想排除HTMLH中的所有内容,只提取HTMLH中的所有可读文本。在您的案例中,您如何定义可读文本,并将其翻译成程序?所有不在或?所有不在和标记中的文本