【发布时间】:2020-10-29 18:50:27
【问题描述】:
好的,我正在上 Pyhton 的课程,作业要求我们从 html 文档中检索数据。 这是我想出的:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
intlist = list()
tot = 0
count = 0
url = input('Enter - ')
html = urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
tags = soup('span')
for tag in tags:
n = tag.contents[0]
n = int(n)
count += 1
tot = tot + n
print("Count:", n)
print("Total:", tot)
当我尝试访问文件时会发生这种情况(注意:我尝试检索的文件存储在本地):
这个错误的原因是什么? 感谢任何人的帮助。
【问题讨论】:
-
你的 html 存储在本地文件系统中吗?
-
那么,协议呢?
http、https、ftp、ftps- 仅举几例?否则只需通过with open(my_file) as file:打开文件 -
@bigbounty 这就是任务要求你做的事情
-
@Jan 试过了,没用