0

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

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'>
4

1 回答 1

1

发出请求并将响应保存到文件:

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()
于 2019-02-25T10:53:05.553 回答