首页 > 解决方案 > BeautifulSoup 的 find_all 方法进入循环

问题描述

我正在用 BeautifulSoup 做一些抓取练习,但我生成了一个似乎在循环中的事件。

这是我的代码:

from bs4 import BeautifulSoup
import requests

# Print all links in the page

linkpage = "https://automatetheboringstuff.com/chapter12/"
page = requests.get(linkpage)
page.econding = "utf-8"
data = page.text
html = BeautifulSoup(data, "html5lib")

for link in html.find_all("a"):
    print(link)

当我执行此脚本时,CPU 达到最大值,没有打印任何内容,并且执行循环进行。为什么?

两个重要的考虑:

编辑:

将解析器更改为 xml 即可。

为什么 html5lib 我有这个问题(现在只在这个特定的页面上)?

标签: pythonbeautifulsoup

解决方案


尝试这个
    从 bs4 导入 BeautifulSoup
    导入请求

    # 打印页面中的所有链接

    链接页=“https://automatetheboringstuff.com/chapter12/”
    page = requests.get(linkpage)
    page.econding = "utf-8"
    数据 = page.text
    html = BeautifulSoup(数据)

    all_link=html.find_all('a')
    all_link 中的链接:
        打印(链接.get('href'))


推荐阅读