【发布时间】:2019-12-01 14:03:49
【问题描述】:
这是我的作业:
在本作业中,您将编写一个类似于http://www.py4e.com/code3/urllink2.py 的 Python 程序。该程序将使用 urllib 从下面的数据文件中读取 HTML,并解析数据,提取数字并计算文件中数字的总和。
我们为此作业提供了两个文件。一个是我们为您提供测试总和的示例文件,另一个是您需要为作业处理的实际数据。
样本数据:http://py4e-data.dr-chuck.net/comments_42.html (Sum=2553)
实际数据:http://py4e-data.dr-chuck.net/comments_228869.html(总和以 10 结尾)
您不需要将这些文件保存到您的文件夹中,因为您的程序会直接从 URL 读取数据。注意:每个学生的作业都有一个不同的数据 url - 因此只能使用您自己的数据 url 进行分析。
我想修复我的代码,因为这是我迄今为止所学到的。我收到一个名称错误
urllib 没有定义
.. 如果我玩进口,那么我的套接字就会有问题。
import urllib
import re
from bs4 import BeautifulSoup
url = input('Enter - ')
html = urlib.request(url, context=ctx).read()
soup = BeautifulSoup(html, "html.parser")
sum=0
# Retrieve all of the anchor tags
tags = soup('span')
for tag in tags:
# Look at the parts of a tag
y=str(tag)
x= re.findall("[0-9]+",y)
for i in x:
i=int(i)
sum=sum+i
print(sum)
【问题讨论】:
-
如果我将其更改为 from urllib.request import urlopen 那么我的模块套接字没有属性为 AF_INET
标签: python beautifulsoup