Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 转换<;请求、模型、响应>;变成对象_Python_Iterator_Response_Httpresponse - Fatal编程技术网

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

我发送了一个请求并收到了如下所示的响应。python类型(响应)等于
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对象”?可用作什么?