这是一个使用Python编写的采集百度问答的脚本,我们定义了一个`get_baidu_search`函数,用于获取百度搜索结果,我们定义了一个`get_baidu_answer`函数,用于获取百度知道的答案,我们在主程序中调用这两个函数,分别输出搜索结果和答案。
import requests from bs4 import BeautifulSoup def get_baidu_search(query, num_results=10): url = f"https://www.baidu.com/s?wd={query}" 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.text, "html.parser") search_results = soup.find_all("h3", class_="t") results = [] for i in range(num_results): if i < len(search_results): title = search_results[i].get_text() link = search_results[i].find("a")["href"] results.append((title, link)) return results def get_baidu_answer(query): url = f"https://zhidao.baidu.com/question/{query}" 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.text, "html.parser") answer = soup.find("div", class_="best-text") if answer: return answer.get_text().strip() else: return "未找到答案" if __name__ == "__main__": query = input("请输入您要查询的问题:") search_results = get_baidu_search(query) print("搜索结果:") for title, link in search_results: print(f"{title} - {link}") answer = get_baidu_answer(query) print(" 百度知道答案:") print(answer)
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/8200.html