我正在使用漂亮的汤抓取html,并使用正则表达式使用USGS网站从两条河流中提取数据。我正在收集河表高度,日期和时间。代码适用于第一个,但不适用于第二个。
from urllib.request import urlopen as uReq
import re
import os
from bs4 import BeautifulSoup as soup
wilson_url = 'https://waterdata.usgs.gov/or/nwis/uv?site_no=14301500'
wilson_client = uReq(wilson_url)
wilson_html = wilson_client.read()
wilson_client.close()
wilson_soup = soup(wilson_html, "html.parser")
wilson = wilson_soup.findAll("div",{"class":"stationContainer"})
wilson_lvl_text = wilson[2].text
gauge_compile = re.compile('Most recent instantaneous value:\s+(\d+\\.\d+\d)+\s+(\d+\d+\\-\d+\d+\\-\d+\d+\d+\d)+\s+\s+\s+(\d+\d+\\:\d+\d+\s+\w+\w+\w)')
gauge_search = gauge_compile.search(wilson_lvl_text)
wilson = float(gauge_search.group(1))
wil_day = gauge_search.group(2)
wil_time = gauge_search.group(3)
print('As of', wil_day, ', at', wil_time, '...')
print()
print('The Wilson River level is', wilson, 'feet.')
nehalem_url = 'https://waterdata.usgs.gov/nwis/uv?site_no=14301000'
nehalem_client = uReq(nehalem_url)
nehalem_html = nehalem_client.read()
nehalem_client.close()
nehalem_soup = soup(nehalem_html, "html.parser")
nehalem = nehalem_soup.findAll("div",{"class":"stationContainer"})
nehalem_lvl_text = nehalem[2].text
gauge_compile = re.compile('Most recent inehantaneous value:\s+(\d+\\.\d+\d)+\s+(\d+\d+\\-\d+\d+\\-\d+\d+\d+\d)+\s+\s+\s+(\d+\d+\\:\d+\d+\s+\w+\w+\w)')
gauge_search = gauge_compile.search(nehalem_lvl_text)
nehalem = float(gauge_search.group(1))
neh_day = gauge_search.group(2)
neh_time = gauge_search.group(3)
print('As of', neh_day, ', at', neh_time, '...')
print()
print('The Nehalem River level is', nehalem, 'feet.')
运行模块会为威尔逊河输出正确的读数,但在尝试使用正则表达式查找尼哈林河水位表读数时会导致错误:
As of 12-10-2019 , at 22:30 PST ...
The Wilson River level is 4.2 feet.
Traceback (most recent call last):
File "C:\Python\Scripts\streams.py", line 41, in <module>
nehalem = float(gauge_search.group(1))
AttributeError: 'NoneType' object has no attribute 'group'
答案 0 :(得分:1)
男人,没有“最近的瞬时值”,而是“最近的瞬时值:”。