【问题标题】:Scraping numbers from HTML using Python and BeautifulSoup使用 Python 和 BeautifulSoup 从 HTML 中抓取数字
【发布时间】: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


【解决方案1】:

错别字:您输入的是urlib,它应该是urllibcontext=ctx 不是必需的:

import re
import urllib
from bs4 import BeautifulSoup

# url = 'http://py4e-data.dr-chuck.net/comments_42.html'
url = 'http://py4e-data.dr-chuck.net/comments_228869.html'

soup = BeautifulSoup(urllib.request.urlopen(url).read(), 'html.parser')
s = sum(int(td.text) for td in soup.select('td:last-child')[1:])

print(s)

打印:

2410

编辑:运行你的脚本:

import urllib.request
import re
from bs4 import BeautifulSoup


html = urllib.request.urlopen('http://py4e-data.dr-chuck.net/comments_228869.html').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)

打印:

2410

【讨论】:

  • 嘿,谢谢您的回答,我将其更改为 double l,现在我收到 urllib 没有属性请求...如果我无法修复我的代码,我将使用一个由你提供,但我会更乐意使用我的
  • 3.7 ...当时我可能混淆了导入
  • 现在当我运行它时它是 'socket' 没有属性 'AF_INET'
  • shiit...这让我很困惑,我在同一个文件夹中有套接字程序,由于某种原因,它发生了一个错误...现在一切都很好,非常好,但是为什么如果我正在运行我的命令提示 python smth.py 错误会从另一个文件中显示出来?那是有线的!
【解决方案2】:
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
import re

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")

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)

【讨论】:

【解决方案3】:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

url = input('Enter - ')
html = urlopen(url,).read()
soup = BeautifulSoup(html, "html.parser")

# Retrieve all of the anchor tags
tags = soup('span')
numlist = list()
for tag in tags:
    # Look at the parts of a tag
    y = str(tag)
    num = re.findall('[0-9]+',y)
    numlist = numlist + num

sum = 0
for i in numlist:
    sum = sum + int(i)

print(sum)

【讨论】:

    猜你喜欢
    • 2016-10-28
    • 2020-06-03
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多