可以使用Python中的requests库和BeautifulSoup库来保存动态加载的网页。下面是一个示例代码:
import requests
from bs4 import BeautifulSoup
url = "https://example.com" # 替换为需要保存的网页URL
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
} # 添加请求头,模拟浏览器访问
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
# 处理动态加载的内容
# ...
# 保存网页
with open("saved_page.html", "w", encoding='utf-8') as file:
file.write(soup.prettify())
在这个示例中,我们首先使用requests库发送请求获取网页内容,然后使用BeautifulSoup库解析网页。如果网页中有动态加载的内容,可以根据实际情况进行处理。最后,使用file.write()方法将网页内容保存到一个名为"saved_page.html"的文件中。
上一篇:保存动态创建的表单
下一篇:保存动态生成的Xamarin控件