1

我想提取粗体文本,它表示来自该网站http://app2.nea.gov.sg/anti-pollution-radiation-protection/air-pollution/psi/psi-readings-over的最新天气 psi -the-last-24-hours。有谁知道如何使用下面的代码提取?

我还需要提取当前天气 psi 前面的两个值来进行计算。总共三个值(最新和前两个值)

示例:当前值(粗体)是 5AM:51,我还需要 3AM 和 4AM。有谁知道并且可以帮助我吗?提前致谢 !

    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)

印刷:

    {'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'}
4

2 回答 2

1

确保您了解这里发生了什么:

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

这将打印3-hr PSI以粗体标记的值和它之前的两个值(如果存在)。

希望有帮助。

于 2013-06-24T18:19:33.060 回答
0

此代码(见带#changed文字的行)

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)

给你

[[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'-']]

print data[4:7]给你

[[u'3AM', u'-'], [u'4AM', u'-'], [u'5AM', u'-']]
于 2013-06-24T17:12:09.707 回答