Python 转换<;请求、模型、响应>;变成对象
我发送了一个请求并收到了如下所示的响应。python类型(响应)等于Python 转换<;请求、模型、响应>;变成对象,python,iterator,response,httpresponse,Python,Iterator,Response,Httpresponse,我发送了一个请求并收到了如下所示的响应。python类型(响应)等于requests.models.response 下面的代码将对象保存到一个html文件中,但我想将其转换为BeautifulSoup python对象(var:html_文件),而不将其保存到本地文件中。我该怎么做?多谢各位 response = requests.get(...) with open(local_filename, 'wb') as f: for chunk in response.it
requests.models.response
下面的代码将对象保存到一个html文件中,但我想将其转换为BeautifulSoup python对象(var:html_文件),而不将其保存到本地文件中。我该怎么做?多谢各位
response = requests.get(...)
with open(local_filename, 'wb') as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
html_file = BeautifulSoup(open(html_file_dir), "html.parser")
使用响应的
.text
属性将数据读取为字符串。如何阅读https://www.google.com
不写入文件:
import requests
from bs4 import BeautifulSoup
url = 'https://www.google.com'
response = requests.get(url)
html_file = BeautifulSoup(response.text, "html.parser")
print(html_file.prettify())
进一步阅读:
是否要将HTML页面读取为字符串?然后你可以做
html\u string=response.text
什么是“可用的Python对象”?可用作什么?