gpt4 book ai didi

python - Beautifulsoup 将字符串转换为 bs4.element 模块的 ResultSet 对象

转载 作者:行者123 更新时间:2023-12-04 08:39:25 25 4
gpt4 key购买 nike

有没有办法将 BeautifulSoup 的 ResultSet 对象保存到文件中,然后读取文件并能够使用 find_all 等命令?
例如,我的代码是

import requests
from bs4 import BeautifulSoup

#scraping
website_link = 'https://stackoverflow.com/'
request1 = requests.get(website_link)
source1 = request1.content
soup1 = BeautifulSoup(source1, 'lxml')


#saving
savefilename = 'question.txt'
with open(savefilename, "w", encoding="utf-8") as f:
f.write(str(soup1))
f.close()
在步骤 f.write(str(soup1)) ,我基本上是将 bs4.element 的 ResultSet 对象转换为字符串以进行保存,这是至关重要的,我还没有找到解决方法。一旦它被转换成一个字符串,有没有办法转换回 BeautifulSoup 的 ResultSet 对象,这将允许我使用 .find_all()和类似的命令?

最佳答案

只需创建另一个 BeautifulSoup目的:

import requests
from bs4 import BeautifulSoup

#scraping
website_link = 'https://stackoverflow.com/'
request1 = requests.get(website_link)
source1 = request1.content
soup1 = BeautifulSoup(source1, 'html.parser')


#saving
savefilename = 'question.txt'
with open(savefilename, "w", encoding="utf-8") as f:
f.write(str(soup1))

# Open the saved file
with open(savefilename, "r", encoding="utf-8") as f:
soup2 = BeautifulSoup(str(f.readlines()), "html.parser")

>>> print(type(soup2))
class 'bs4.BeautifulSoup'>

关于python - Beautifulsoup 将字符串转换为 bs4.element 模块的 ResultSet 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64633925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com