【问题标题】:How can I clean html code to return only the number values?如何清理 html 代码以仅返回数字值?
【发布时间】:2020-10-19 02:42:27
【问题描述】:
<div class="bb-fl" style="background:Tomato;width:0.63px" title="10">​</div>,
<div class="bb-fl" style="background:SkyBlue;width:0.19px" title="3">​</div>,
<div class="bb-fl" style="background:Tomato;width:1.14px" title="18">​</div>,
<div class="bb-fl" style="background:SkyBlue;width:0.19px" title="3">​</div>,
<div class="bb-fl" style="background:Tomato;width:1.52px" title="24">​</div>,

我目前有一个列表中的上述 html 代码。我希望使用 python,以便它可以输出以下内容,然后附加到列表中:

10
3
18
3
24

【问题讨论】:

  • 你尝试过做什么?
  • @adrianp 我尝试使用正则表达式来清理文本。我尝试使用正则表达式删除文本
  • 如果这些解决方案中的任何一个解决了您的问题,请接受它。

标签: python html regex web-scraping text


【解决方案1】:

这里有 3 种可能性。在前 2 个版本中,我们确保类在将其附加到列表之前签出 - 以防万一您不想包含其他 div。在第三种方法中,没有真正的好方法来做到这一点。与 adrianp 的拆分方法不同,我不在乎标题在哪里。

第三种方法可能有点混乱,所以请允许我解释一下。首先,我们在出现title=" 的所有地方进行拆分。我们转储该列表的第一个索引,因为它是第一个标题之前的所有内容。然后我们循环剩余部分并在第一个引号上拆分。现在您想要的数字在该拆分的第一个索引中。我们执行内联弹出来获取该值,因此我们可以将所有内容保存在列表推导中,而不是扩展整个循环并使用特定索引来争夺值。

要远程加载 html,请取消注释 html var 并将“yourURL”替换为适合您的。

我想我已经为你提供了所有可能的方法——当然是最明显的方法。

from bs4 import BeautifulSoup
import re, requests

html = '<div class="bb-fl" style="background:Tomato;width:0.63px" title="10">​</div> \
<div class="bb-fl" style="background:SkyBlue;width:0.19px" title="3">​</div> \
<div class="bb-fl" style="background:Tomato;width:1.14px" title="18">​</div> \
<div class="bb-fl" style="background:SkyBlue;width:0.19px" title="3">​</div> \
<div class="bb-fl" style="background:Tomato;width:1.52px" title="24">​</div>'

#html = requests.get(yourURL).content

# possibility 1: BeautifulSoup

soup = BeautifulSoup(html, 'html.parser')

# assumes that all bb-fl classed divs have a title and all divs have a class
# you may need to disassemble this generator and add some extra checks
bs_titleval = [div['title'] for div in soup.find_all('div') if 'bb-fl' in div['class']]
  
print(bs_titleval)


# possibility 2: Regular Expressions ~ not the best way to go
# this isn't going to work if the tag attribute signature changes

title_re = re.compile('<div class="bb-fl" style="[^"]*" title="([0-9]+)">', re.I)

re_titleval = [m.group(1) for m in title_re.finditer(html)]
    
print(re_titleval)


# possibility 3: String Splitting ~ 
# probably the best method if there is nothing extra to weed out

title_sp = html.split('title="')
title_sp.pop(0) # get rid of first index

# title_sp is now ['10"></div>...', '3"></div>...', '18"></div>...', etc...]
sp_titleval = [s.split('"').pop(0) for s in title_sp]

print(sp_titleval)

【讨论】:

    【解决方案2】:

    我会推荐使用 Beautiful Soup,它是一个非常流行的 html 解析模块,非常适合这种事情。如果每个元素都有title 的属性,那么你可以这样做:

    from bs4 import BeautifulSoup
    import requests
    def randomFacts(url):
        r = requests.get(url)
        bs = BeautifulSoup(r.content, 'html.parser')
        title = bs.find_all('div')
        for each in title:
            print(each['title'])
    

    Beautiful Soup 是我常用的 html 解析工具,希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      假设每个div都以字符串形式保存在变量div中,可以做如下操作:

      number = div.split()[3].split('=')[1]
      

      每个 div 的格式都应该相同。

      【讨论】:

      • 这行不通。使用该方法会返回"10"&gt;&lt;/div&gt;等数据
      猜你喜欢
      • 2018-02-23
      • 2012-01-25
      • 1970-01-01
      • 2014-01-18
      • 2021-01-13
      • 2017-09-22
      • 1970-01-01
      • 2017-06-25
      • 2011-06-20
      相关资源
      最近更新 更多