将html响应保存并加载为文件中的对象 - python

问题描述 投票:0回答:1

我的目标是保存并加载响应以供以后使用。 (这可能是几天后)。我想要高效,不要随时抓取网站,而是将它们保存为文件并在需要时加载它们。

import requests
from bs4 import BeautifulSoup

html = requests.get(link, timeout=5)
page_content = BeautifulSoup(html.content, "html.parser")

"""" Example of function that I would like to load the responce here instead."""

def getValue(page):
    return page.find('td', attrs={'class': 'Fz(s) Fw(500) Ta(end)'}).text

html的类型:

<class 'requests.models.Response'>

page_content的类型:

<class 'bs4.BeautifulSoup'>
python object python-requests python-object
1个回答
0
投票

发出请求并保存对文件的响应:

import requests

r = requests.get(url)
content = r.text

with open('workfile', 'w') as f:
    f.write(content)

当您需要访问以前保存的数据时:

with open('workfile') as f:
   read_data = f.read()
© www.soinside.com 2019 - 2024. All rights reserved.