Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.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 删除没有定义元素的网页_Python_Html_Web Scraping - Fatal编程技术网

Python 删除没有定义元素的网页

Python 删除没有定义元素的网页,python,html,web-scraping,Python,Html,Web Scraping,我正在尝试用python创建一个非常简单的网页 我在scraping上找到的大多数说明都指示您查看源代码以确定结构,然后使用“元素”访问所需的信息。但是,我正在使用的网页格式很差,没有在线示例中描述的元素类型 我的网页如下所示: <html> <head></head> <body> "Title 1, Title 2, Title 3" <br> "Info 1, Info 2, Info 3"

我正在尝试用python创建一个非常简单的网页

我在scraping上找到的大多数说明都指示您查看源代码以确定结构,然后使用“元素”访问所需的信息。但是,我正在使用的网页格式很差,没有在线示例中描述的元素类型

我的网页如下所示:

<html>
  <head></head>
  <body>
    "Title 1, Title 2, Title 3"
    <br>
    "Info 1, Info 2, Info 3"
    <br>
    "Info 1, Info 2, Info 3"
    <br>
  </body>
</html>

标题1、标题2、标题3

信息1、信息2、信息3
信息1、信息2、信息3
我希望能够把所有这些都读入一个excel文件,每一行信息都作为一个新行,每一条信息都在不同的列中。有什么建议吗


谢谢你的帮助

使用
lxml
XPath

from lxml import html

HTML = """<html>
  <head></head>
  <body>
    "Title 1, Title 2, Title 3"
    <br>
    "Info 1, Info 2, Info 3"
    <br>
    "Info 1, Info 2, Info 3"
    <br>
  </body>
</html>"""

tree = html.fromstring(HTML)
results = [x.strip() for x in tree.xpath('//body/text()')]
print results

最后,您可以使用
csv
library编写一个可以用Excel打开的csv格式文件

我想这样开始:抓住整个
(即将
作为在线示例中描述的元素之一)

然后将整个字符串保存为文本文件。(如果您使用
csv
作为扩展,您可以立即用Excel打开它。)

在Excel中打开它。如果幸运的话,逗号被视为列分隔符,就这样!(只需要去掉那些

,但这应该不是什么大问题)

[
  '"Title 1, Title 2, Title 3"',
  '"Info 1, Info 2, Info 3"',
  '"Info 1, Info 2, Info 3"',
  ''
]