python - 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 达到最大值,没有打印任何内容,并且执行循环进行。为什么?
两个重要的考虑:
这仅在 Linux 下发生(使用 Python 2 和 3)。我在 Windows 下没有得到相同的行为:效果很好,所有链接都打印正确!:-|
这只发生在变量linkpage中指示的 URL 上。当我与其他人(即https://stackoverflow.com/)更改它时,它可以正常工作。
编辑:
将解析器更改为 xml 即可。
为什么 html5lib 我有这个问题(现在只在这个特定的页面上)?
解决方案
尝试这个
从 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'))
推荐阅读
- r - 如何使用条件(如果或同时)替换 R 中的值?
- python - 使用 m.CV 与 m.Var
- python - 有没有一种优雅的方法可以使用自动生成的参数调用机器人框架测试?
- markdown - Gauge:在规范之前运行背景场景(Concept.cpt)
- javascript - React Redux 不更新状态
- azure - 如何让 2 个不同的 kubernetes 集群中的 2 个服务在 azure 中安全地相互通信?
- mysql - mysql - 将列合并为一行
- assembly - 如何在不使用 MIPS 中的数组的情况下存储多个用户输入的整数?
- r - 将样本大小数据添加到 R 中的饼图(mapplots 包)
- macos - 将源提取到静态库会增加大小