Web scraping 是否有任何方法可以获得所有头链接的输出,因为iv没有,也没有错误

Web scraping 是否有任何方法可以获得所有头链接的输出,因为iv没有,也没有错误,web-scraping,beautifulsoup,Web Scraping,Beautifulsoup,尝试使用BeautifulSoup从Bing中删除标题链接,但我没有收到任何错误或输出 from bs4 import BeautifulSoup import requests search = input("Search for:") params = {"q": search} r = requests.get("http://www.bing.com/search", params=params) soup = Beauti

尝试使用BeautifulSoup从Bing中删除标题链接,但我没有收到任何错误或输出

from bs4 import BeautifulSoup
import requests

search = input("Search for:")
params = {"q": search}
r = requests.get("http://www.bing.com/search", params=params)

soup = BeautifulSoup(r.text, "html.parser")
results = soup.find("ol", {"id": "b_results"})
links = soup.findAll("li", {"class": "b_algo"})

for item in links:
    item_text = item.find("a").text
    item_href = item.find("a").attrs["href"]

    if item_text and item_href:
        print(item_text)
        print(item_href)

尝试指定
User-Agent
HTTP头以获得结果:

import requests
from bs4 import BeautifulSoup


url = 'https://www.bing.com/search'
params = {'q': 'tree'}
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'}
soup = BeautifulSoup(requests.get(url, headers=headers, params=params).content, 'html.parser')

for a in soup.select('.b_algo a'):
    print(a.text, a['href'])
印刷品:

tree|好きな物語と出逢えるサイト https://tree-novel.com/
sustainably stylish home furniture Hong Kong | TREE https://tree.com.hk/
Chairs & Benches https://tree.com.hk/furniture/chairs-benches
Desks https://tree.com.hk/furniture/desks
Living Room https://tree.com.hk/rooms/living-room
Bedroom https://tree.com.hk/rooms/bedroom
Finishing Touches https://tree.com.hk/furniture/finishing-touches
Entryway https://tree.com.hk/rooms/entryway
Tree | Definition of Tree by Merriam-Webster https://www.merriam-webster.com/dictionary/tree
Tree | Definition of Tree at Dictionary.com https://www.dictionary.com/browse/tree
tree | Structure, Uses, Importance, & Facts | Britannica https://www.britannica.com/plant/tree
Tree Images · Nature Photography · Free Photos from Pexels ... https://www.pexels.com/search/tree/

你知道我的代码有什么问题吗?因为我的朋友有同样的一个,他的工作非常好,而我的只是没有产生任何输出。@SanskarB.C。问题是您必须在
requests.get()中指定
User-Agent
HTTP头文件={'User-Agent':'Mozilla/5.0(X11;Ubuntu;Linux x86_64;rv:80.0)Gecko/20100101 Firefox/80.0'。。你是说。。。所以这就像是一个请求的通用头。get或者它总是不同的。。我从来没有见过这样的头球。。我有点confused@SanskarB.C. 这就是HTTP协议的工作原理。浏览器将向服务器发送请求(带有一些标题,其中包括标识浏览器的
User Agent
标题)。默认情况下,
请求
向服务器发送不同的头,服务器的响应与传统浏览器不同。Mozilla/5.0(Windows NT 6.1;Win64;x64;rv:47.0)Gecko/20100101 Firefox/47.0。。我可以使用这个用户代理作为标题吗?