以下是一个示例代码,它将从指定网址下载HTML内容,将其存储在列表中,然后将其保存到本地文件,并将其转换为Python列表:

import requests
from bs4 import BeautifulSoup
import json

# URL to fetch HTML from
url = "https://www.example.com"

# Fetch the HTML content
response = requests.get(url)
html = response.content

# Parse the HTML using BeautifulSoup
soup = BeautifulSoup(html, "html.parser")

# Find all the <a> tags and store the href attribute in a list
links = []
for link in soup.find_all('a'):
    links.append(link.get('href'))

# Save the list to a file in JSON format
with open('links.json', 'w') as f:
    json.dump(links, f)

# Load the list from the file
with open('links.json', 'r') as f:
    links = json.load(f)

# Print the list to verify it was loaded correctly
print(links)

在这个例子中,我们使用requests库来获取HTML内容,并使用BeautifulSoup库来解析HTML。然后,我们使用Python列表来存储所有元素的href属性,并将其保存到一个名为“links.json”的文件中。最后,我们使用json库从文件中加载列表,并将其打印出来以验证它是否正确加载了。

用python写一段代码把html网页存入列表然后保存到本地然后再转换为列表

原文地址: https://www.cveoy.top/t/topic/gei 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录