Beautiful Soup 没有返回我预期的任何东西

Beautiful Soup not returning anything I expected

背景: 接下来是 Udemy 教程,该教程正在解析来自 Bing 的一些信息。 它接收用户输入并将其用作搜索 Bing 的参数,返回它可以在第一页

上找到的所有 href 链接

代码:

from bs4 import BeautifulSoup
import requests as re

search = input("Enter what you wanna search: \n")
params = {"q": search}
r = re.get("https://www.bing.com/search", params=params)

soup = BeautifulSoup(r.text, 'html.parser')

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


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

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

    else:
        print("Couldn't find 'a' or 'href'")

问题: 它returns没什么。该代码显然对他有用。我没有收到任何错误,因为我检查了 idclass 名称,看看自从制作视频以来它们在 bing 上是否已经更改,但它们仍然相同

"ol",{"id":"b_results"}
"li",{"class": "b_algo"}

有什么想法吗?我是网络抓取的完全菜鸟,但处于 Python.

的中间水平

提前致谢!

您的脚本运行良好。如果您仔细查看请求的答案(例如将 r.text 保存到文件中),您会看到答案充满了 javascript.

按照这个方法,你会看到身上布满了<script>个应答器:

<!DOCTYPE html>
<body>
<script>(...)</script>
<script>(...)</script>
<script>(...)</script>
</body>
</html>

我建议尝试另一个网站,或者使用 Selenium。 Udemy 真的要求尝试抓取 bing.com 吗?

您的代码需要一些修改。

首先,你需要 headers,否则 Bing(正确地)认为你是一个机器人,它不会返回 HTML

然后,您需要检查锚点是否不是 None,并且在 href 中是否至少有 http

例如:

from bs4 import BeautifulSoup
import requests


headers = {
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36",
}
page = requests.get("https://www.bing.com/search?", headers=headers, params={"q": "python"}).text
soup = BeautifulSoup(page, 'html.parser')

anchors = soup.find_all("a")
for anchor in anchors:
    if anchor is not None:
        try:
            if "http" in anchor["href"]:
                print(anchor.getText(), anchor["href"])
        except KeyError:
            continue

输出:

Welcome to Python.org https://www.python.org/
Diese Seite übersetzen http://www.microsofttranslator.com/bv.aspx?ref=SERP&br=ro&mkt=de-DE&dl=de&lp=EN_DE&a=https%3a%2f%2fwww.python.org%2f
Python Downloads https://www.python.org/downloads/
Windows https://www.python.org/downloads/windows/
Python for Beginners https://www.python.org/about/gettingstarted/
About https://www.python.org/about/
Documentation https://www.python.org/doc/
Community https://www.python.org/community/
Success Stories https://www.python.org/success-stories/
News https://www.python.org/blogs/
Python (Programmiersprache) – Wikipedia https://de.wikipedia.org/wiki/Python_%28Programmiersprache%29
Wikipedia https://de.wikipedia.org/wiki/Python_%28Programmiersprache%29
CC-BY-SA-Lizenz http://creativecommons.org/licenses/by-sa/3.0/
Python lernen - Python Kurs für Anfänger und Fortgeschrittene https://www.python-lernen.de/
Python 3.9.0 (64bit) für Windows - Download https://python.de.uptodown.com/windows
Python-Tutorial: Tutorial für Anfänger und Fortgeschrittene https://www.python-kurs.eu/kurs.php
Mehr zu python-kurs.eu anzeigen https://www.python-kurs.eu/kurs.php
Python (Programmiersprache) – Wikipedia https://de.wikipedia.org/wiki/Python_%28Programmiersprache%29
Python (Programmiersprache) - Wikipedia https://de.wikipedia.org/wiki/Python_%28Programmiersprache%29

顺便问一下,这是什么课程,因为抓取搜索引擎并不容易?