[Python] 纯文本查看 复制代码import requests
from bs4 import BeautifulSoup
# 设置搜索关键字和页面数
keyword = "海洋生态保护修复"
page_count = 10
# 构造搜索页面的URL
base_url = "https://www.pkulaw.com/search/"
params = {
"searchKeyword": keyword,
"fullText": "true",
"range": "text",
"library": "pflaw",
"pageIndex": 1,
"pageSize": 20,
"highlight": "true"
}
# 发送搜索请求,获取搜索结果
results = []
for i in range(1, page_count + 1):
params["pageIndex"] = i
response = requests.get(base_url, params=params)
soup = BeautifulSoup(response.text, "html.parser")
items = soup.select(".search-result .result-list .item")
for item in items:
title = item.select_one(".title a").text.strip()
url = item.select_one(".title a")["href"]
content = item.select_one(".content .text").text.strip()
results.append((title, url, content))
# 将搜索结果导出为文本文件
with open("result.txt", "w", encoding="utf-8") as f:
for title, url, content in results:
f.write(title + "\n")
f.write(url + "\n")
f.write(content + "\n\n")