我的目标是保存并加载响应以供以后使用。 (这可能是几天后)。我想要高效,不要随时抓取网站,而是将它们保存为文件并在需要时加载它们。
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'>
发出请求并保存对文件的响应:
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()