python beautifulsoup extracting text

2019-02-28 08:09发布

问题:

I would like to extract the bold text, which is indicating the latest weather psi from this website http://app2.nea.gov.sg/anti-pollution-radiation-protection/air-pollution/psi/psi-readings-over-the-last-24-hours. Does anyone know how to extract using this code below ?

Also I needed to extract two values that is infront of the current weather psi to do calculate. Total of three value (latest and previous two values)

Example: current value (bold) is 5AM : 51, I need also 3AM and 4AM. Does anyone knows and can help me with this ? Thanks in advance !

    from pprint import pprint
    import urllib2
    from bs4 import BeautifulSoup as soup


    url = "http://app2.nea.gov.sg/anti-pollution-radiation-protection/air-pollution/psi/psi-readings-over-the-last-24-hours"
    web_soup = soup(urllib2.urlopen(url))

    table = web_soup.find(name="div", attrs={'class': 'c1'}).find_all(name="div")[2].find_all('table')[0]

    table_rows = []
    for row in table.find_all('tr'):
        table_rows.append([td.text.strip() for td in row.find_all('td')])

    data = {}
    for tr_index, tr in enumerate(table_rows):
        if tr_index % 2 == 0:
            for td_index, td in enumerate(tr):
                data[td] = table_rows[tr_index + 1][td_index]

    pprint(data)

prints:

    {'10AM': '49',
     '10PM': '-',
     '11AM': '52',
     '11PM': '-',
     '12AM': '76',
     '12PM': '54',
     '1AM': '70',
     '1PM': '59',
     '2AM': '64',
     '2PM': '65',
     '3AM': '59',
     '3PM': '72',
     '4AM': '54',
     '4PM': '79',
     '5AM': '51',
     '5PM': '82',
     '6AM': '48',
     '6PM': '79',
     '7AM': '47',
     '7PM': '-',
     '8AM': '47',
     '8PM': '-',
     '9AM': '47',
     '9PM': '-',
     'Time': '3-hr PSI'}

回答1:

Make sure you understand what is going on here:

import urllib2
import datetime

from bs4 import BeautifulSoup as soup


url = "http://app2.nea.gov.sg/anti-pollution-radiation-protection/air-pollution/psi/psi-readings-over-the-last-24-hours"
web_soup = soup(urllib2.urlopen(url))

table = web_soup.find(name="div", attrs={'class': 'c1'}).find_all(name="div")[2].find_all('table')[0]

data = {}
bold_time = ''
cur_time = datetime.datetime.strptime("12AM", "%I%p")
for tr_index, tr in enumerate(table.find_all('tr')):
    if 'Time' in tr.text:
        continue
    for td_index, td in enumerate(tr.find_all('td')):
        if not td_index:
            continue
        data[cur_time] = td.text.strip()
        if td.find('strong'):
            bold_time = cur_time
        cur_time += datetime.timedelta(hours=1)

print data.get(bold_time)  # bold
print data.get(bold_time - datetime.timedelta(hours=1))  # before bold
print data.get(bold_time - datetime.timedelta(hours=2))  # before before bold

This will print the 3-hr PSI value that is marked in bold and two values before it (if exist).

Hope that helps.



回答2:

This code (see lines with #changed text)

from pprint import pprint
import urllib2
from bs4 import BeautifulSoup as soup


url = "http://app2.nea.gov.sg/anti-pollution-radiation-protection/air-pollution/psi/psi-readings-over-the-last-24-hours"
web_soup = soup(urllib2.urlopen(url))

table = web_soup.find(name="div", attrs={'class': 'c1'}).find_all(name="div")[2].find_all('table')[0]

table_rows = []
for row in table.find_all('tr'):
    table_rows.append([td.text.strip() for td in row.find_all('td')])

data = [] # changed
for tr_index, tr in enumerate(table_rows):
    if tr_index % 2 == 0:
        for td_index, td in enumerate(tr):
            data.append([td, table_rows[tr_index + 1][td_index]]) # changed

pprint(data)

give you

[[u'Time', u'3-hr PSI'],
 [u'12AM', u'57'],
 [u'1AM', u'-'],
 [u'2AM', u'-'],
 [u'3AM', u'-'],
 [u'4AM', u'-'],
 [u'5AM', u'-'],
 [u'6AM', u'-'],
 [u'7AM', u'-'],
 [u'8AM', u'-'],
 [u'9AM', u'-'],
 [u'10AM', u'-'],
 [u'11AM', u'-'],
 [u'Time', u'3-hr PSI'],
 [u'12PM', u'-'],
 [u'1PM', u'-'],
 [u'2PM', u'-'],
 [u'3PM', u'-'],
 [u'4PM', u'-'],
 [u'5PM', u'-'],
 [u'6PM', u'-'],
 [u'7PM', u'-'],
 [u'8PM', u'-'],
 [u'9PM', u'-'],
 [u'10PM', u'-'],
 [u'11PM', u'-']]

and print data[4:7] give you

[[u'3AM', u'-'], [u'4AM', u'-'], [u'5AM', u'-']]