要解决Beautiful Soup每次返回相同的输出的问题,可以使用以下方法:
from bs4 import BeautifulSoup
with open("example.html", "r") as f:
html = f.read()
soup = BeautifulSoup(html, "html.parser")
# 进行解析和处理
import requests
from bs4 import BeautifulSoup
url = "http://example.com"
headers = {"User-Agent": "Mozilla/5.0"}
response = requests.get(url, headers=headers)
html = response.content
soup = BeautifulSoup(html, "html.parser")
# 进行解析和处理
import random
from bs4 import BeautifulSoup
random.seed(123) # 设置随机种子
html_files = ["file1.html", "file2.html", "file3.html"]
html_file = random.choice(html_files)
with open(html_file, "r") as f:
html = f.read()
soup = BeautifulSoup(html, "html.parser")
# 进行解析和处理
通过以上方法,你可以确保每次运行Beautiful Soup时都得到相同的输出。