以下是一个使用Python编写的必应爬虫导航的示例代码:
import requests
from bs4 import BeautifulSoup
def bing_spider(query):
url = f"https://cn.bing.com/search?q={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.content, "html.parser")
results = soup.find_all("li", class_="b_algo")
for result in results:
title = result.find("h2").text
link = result.find("a")["href"]
print(f"{title}: {link}")
print()
# 示例使用
query = "必应爬虫导航"
bing_spider(query)
这个示例代码使用requests库发送HTTP请求获取必应搜索结果页面的HTML内容,然后使用BeautifulSoup库解析HTML内容,提取出搜索结果中的标题和链接信息,并打印出来。你可以将query
变量的值设置为你想要搜索的关键词。
上一篇:必应浏览器解析器