如何从数组中获取数字

时间:2018-07-31 02:33:42

标签: python-3.x beautifulsoup

[<div class="ticket_type">‐ Help With Steam Workshop<a 
 href="javascript: 
 jsTicketsLast7Days.getOptions().appendValueToParam( 'requestid', 
 '29' ); jsTicketsLast7Days.getOptions().showSelectedRange( true 
  ); $J('#TicketsLast7Days').get(0).scrollIntoView();"> + </a> 
 </div>,
 <div class="ticket_last_24 report_table_right">
                                <span>15</span>
                                <span>(</span><span 
  class="change_increase">+36%</span><span>)</span>                          
 </div>,
 <div class="ticket_last_week report_table_right">
                                <span>271</span>
                                <span>(</span><span 
   class="change_increase">+632%</span><span>)</span>                            
 </div>,
 <div class="ticket_waiting_not_elevated 
 report_table_right">0</div>,
 <div class="ticket_waiting_elevated 
 report_table_right">37</div>,
 []]

我如何只能获取“ ticket_waiting_not_elevated report_table_right”和ticket_waiting_elevated report_table_right数字0和37?

2 个答案:

答案 0 :(得分:0)

也许可以帮忙,

text ="""[<div class="ticket_type">‐ Help With Steam Workshop<a href="javascript: jsTicketsLast7Days.getOptions().appendValueToParam( 'requestid', '29' ); jsTicketsLast7Days.getOptions().showSelectedRange( true ); $J('#TicketsLast7Days').get(0).scrollIntoView();"> + </a></div>,
         <div class="ticket_last_24 report_table_right"><span>15</span><span>(</span><span class="change_increase">+36%</span><span>)</span> </div>,
         <div class="ticket_last_week report_table_right"> <span>271</span><span>(</span><span class="change_increase">+632%</span><span>)</span></div>,
         <div class="ticket_waiting_not_elevated report_table_right">0</div>,
         <div class="ticket_waiting_elevated report_table_right">37</div>,
        []]"""

    soup = BeautifulSoup(text, 'html.parser')
    for i in soup.find_all('div', attrs={'class': ['ticket_waiting_not_elevated report_table_right', 'ticket_waiting_elevated report_table_right']}):
        print(i.get('class')[0], ':', i.text)

# Output is: ticket_waiting_not_elevated : 0
#            ticket_waiting_elevated : 37

答案 1 :(得分:0)

您可以使用select()来获取数据:

data = """[<div class="ticket_type">‐ Help With Steam Workshop<a
 href="javascript:
 jsTicketsLast7Days.getOptions().appendValueToParam( 'requestid',
 '29' ); jsTicketsLast7Days.getOptions().showSelectedRange( true
  ); $J('#TicketsLast7Days').get(0).scrollIntoView();"> + </a>
 </div>,
 <div class="ticket_last_24 report_table_right">
                                <span>15</span>
                                <span>(</span><span
  class="change_increase">+36%</span><span>)</span>
 </div>,
 <div class="ticket_last_week report_table_right">
                                <span>271</span>
                                <span>(</span><span
   class="change_increase">+632%</span><span>)</span>
 </div>,
 <div class="ticket_waiting_not_elevated
 report_table_right">0</div>,
 <div class="ticket_waiting_elevated
 report_table_right">37</div>,
 []]"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'lxml')

nums = soup.select('''div.ticket_waiting_not_elevated.report_table_right,
                      div.ticket_waiting_elevated.report_table_right''')

print([num.text for num in nums])

打印:

['0', '37']

soup.select('div.ticket_waiting_not_elevated.report_table_right, div.ticket_waiting_elevated.report_table_right')选择具有ticket_waiting_not_elevated report_table_right类或ticket_waiting_elevated report_table_right类的所有div。