web-scraping - 有什么方法可以得到所有标题链接的输出,因为 iv 没有也没有错误
问题描述
尝试使用漂亮的汤从 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/
推荐阅读
- r - 在ggplot2的图例中指定形状
- php - 如何将值插入表一次
- sql-server - SQL Server 中 patindex 中的 ^ 有什么用?
- typescript - Dart - 如何强制映射键包含在列表字符串中
- python - 为什么'_io.BufferedRandom'对象没有属性'resize'
- wavesplatform - 在 Wave 钱包中手动签署消息的选项?
- scala - 在火花中联合后再次排序的蜂巢表排序
- python - 如果元组包含子字符串,则识别元组列表中的元组
- python - Python 中的弹性搜索。如何从搜索中获取关闭整数
- python - 如果 dict 键的值在 otherlist 中,则从列表中的 dicts 中删除元素