【问题标题】:When I parse a large XML sitemap on Beautifulsoup in Python, it only parses part of the file当我在 Python 中解析 Beautifulsoup 上的大型 XML 站点地图时,它只解析了文件的一部分
【发布时间】:2022-11-28 11:04:12
【问题描述】:

我编写了使用 Beautiful Soup 提取非常大的站点地图 xml 文件 (10mb) 的 URL 的代码,它完全按照我想要的方式工作,但它似乎只处理了整个文件的一小部分。这是我的代码:

`sitemap = "sitemap1.xml"
from bs4 import BeautifulSoup as bs
import lxml
content = []

with open(sitemap, "r") as file:
    # Read each line in the file, readlines() returns a list of lines
    content = file.readlines()
    # Combine the lines in the list into a string
    content = "".join(content)
    bs_content = bs(content, "xml")

result = bs_content.find_all("loc")

for result in result:
    print(result.text)
`

我已经更改了我的 IDE 以允许更大的文件,它似乎只是在 XML 文件末尾的随机点开始该过程,并且只从那里提取。

【问题讨论】:

标签: python-3.x xml beautifulsoup sitemap


【解决方案1】:

我只是想说我最终解决了这个问题。我在 pandas 中使用了 read XML 函数,它运行良好。原始 XML 文件已损坏。

...我还意识到控制台只是从某个点开始打印,因为它太大了,而且它实际上仍在处理整个文件。

对此感到抱歉 - 我是新来的 :)

【讨论】:

    猜你喜欢
    • 2020-09-12
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    相关资源
    最近更新 更多