【问题标题】:BeautifulSoup find_all() is returning empty listBeautifulSoup find_all() 正在返回空列表
【发布时间】:2020-08-07 03:25:46
【问题描述】:

我对编程很陌生。目前,我正在通过 Udemy 课程学习 Python。我正在使用 Windows 10 操作系统,并且正在使用带有 Anaconda 解释器的 VS Code:Here it is


import requests
from bs4 import BeautifulSoup

url = "https://yellowpages.com.tr/ara?q=ankara"

response = requests.get(url)

html_content = response.content

soup = BeautifulSoup(html_content, "html.parser")

print(soup.find_all("a"))

我编写此代码是为了吸引网站中的所有“a”字符。但是,当我想运行这段代码时,它返回一个空列表:

输出:

[]

我该如何解决这个问题?谢谢你的回答。

【问题讨论】:

  • 请张贴网址和您要提取的内容
  • 如果您发布链接,什么都不会发生。你为什么将你使用的 Python 版本作为图片发布?
  • 好的,我发送了链接。我发送了我的 Python 版本,因为我不知道它是否需要。
  • 这是一个基于 javascript 的网站。你最好使用硒
  • 好的,非常感谢。但是在我的 Python 课程中,老师运行了我发布的代码,并且它没有任何问题。我不明白这种情况。

标签: python beautifulsoup


【解决方案1】:

也许你正在解析的 url 没有链接,我试过一个有 url 的链接,它工作正常

import requests
from bs4 import BeautifulSoup

url = "https://cloud.google.com/solutions/media-entertainment/optimizing-audio-files-for-speech-to-text?hl=it"
response = requests.get(url)
html_content = response.content

soup = BeautifulSoup(html_content, "html.parser")

print(soup.find_all("a"))

【讨论】:

  • 非常感谢。我试过你的代码,它奏效了。但是在我的 Python 课程中,老师尝试了我发布的代码,并且成功了。
  • 但如果我使用curl 下载该站点,它们就不存在。该网站正在使用 JavaScript 来加载它们,但我不确定它是如何让它出现在源代码中的。我在任何地方都没有看到document.write
  • 好吧,javascript 可以做任何事情,所以不需要'document.write'。
猜你喜欢
  • 2019-12-12
  • 2019-03-25
  • 2021-10-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多