【发布时间】:2021-09-27 08:16:23
【问题描述】:
我在 coursera 上为所有人开设了一门 Python 课程。我被困在第 4 周的第 3 门课程上。
我的问题是,当我尝试运行 Beautifulsoup 时,它有一个回溯。我正在上传 2 张图片,一张是我的代码,一张是我的回溯。我尝试从网上下载它,从课程文件夹中尝试过,然后从命令提示符下使用 python -m pip install beautifulsoup 进行尝试。没有一个工作。我真的需要知道如何运行 BeautifulSoup,因为我必须尽快提交我的代码。因此,如果您能解释如何运行 BeautifulSoup,我将不胜感激。 (如果您解释回溯,也将不胜感激!)
谢谢!
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = input('Enter - ')
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
print(soup.find_all("span"))
#tags = soup('a')
#for tag in tags:
#print('TAG:', tag)
#print('URL:', tag.get('href', None))
#print('Contents:', tag.contents[0])
#print('Attrs:', tag.attrs)
【问题讨论】:
-
请粘贴您的错误和代码的实际文本,而不仅仅是屏幕截图。
-
当然。你看不出来吗?
-
我可以;但是,我无法复制/粘贴它来复制和调查,而且它也不适合屏幕阅读器和其他辅助工具。
-
这里的代码sn-p是用Python3和美汤4,能验证一下你的环境吗?
-
你说的“环境”是什么意思?
标签: beautifulsoup