用python写一段代码把html响应存入列表然后保存到本地然后再转换为列表
这里提供一种基于requests库的实现方式:
import requests
from bs4 import BeautifulSoup
# 发送请求,获取html响应
url = "https://www.example.com"
response = requests.get(url)
# 使用BeautifulSoup解析html响应,并提取需要的信息
soup = BeautifulSoup(response.content, 'html.parser')
info_list = soup.find_all('div', class_='info') # 假设需要提取class为info的div标签信息
# 将提取的信息存入列表
result_list = []
for info in info_list:
result_list.append(info.text)
# 保存到本地
with open('result.txt', 'w', encoding='utf-8') as f:
f.writelines(result_list)
# 读取本地文件,转换为列表
with open('result.txt', 'r', encoding='utf-8') as f:
result_list = f.readlines()
以上代码中,使用requests库发送请求,获取html响应。然后使用BeautifulSoup对响应进行解析,并提取需要的信息。将提取的信息存入列表,并使用文件操作将列表保存到本地。最后再读取本地文件,将其转换为列表。
原文地址: https://www.cveoy.top/t/topic/gec 著作权归作者所有。请勿转载和采集!