我抢的文本文件,如使用python如下,从网页。我抓取的数据包含了我不需要的额外事物。我只需要粗体部分。我还需要将每个粗体部分彼此分开。你能帮我这么做吗?在一幅图像中,红色部分也是我试图从数据中提取的部分。蟒蛇文本解析和分裂
[
'\n249\nSRUS54 KFWD 051849\nRR5FWD\n:\n:
ALERT HOURLY ACCUMULATOR DATA\n:
NATIONAL WEATHER SERVICE FORT WORTH TX\n:
**1249 PM CST SUN MAR 5 2017**\n:\n:
HOURLY ACCUMULATOR INFORMATION TABLE\n:\n:
NOTE: ERRONEOU S REPORTS MAY BE RECEIVED UNDER CERTAIN\n:
WEATHER CONDITIONS\n:\n:
**********************************************************\n:
ID LOCATION ACCUMULATOR VALUE\n:
**********************************************************\n:
**CITY OF DALLAS ALERT SYSTEM**
\n**.A DCQT2 170305 C DH124216 /HGIRS
396.7**:
\n\n**.A DCVT2 170305 C DH123434 /HGIRS 516.8**:
\n\n**.A DAOT2 170305 C DH123721 /HGIRS 534.2**:\n\n**.A DDCT2
170305 C DH120338 /HGIRS 395.0**:\n\n**.A DAHT2 170305 C DH114758 /HGIRS
496.1**:\n\n\n\n']
This is an image of the data I grab from the web
import urllib
import re
htmlfile=urllib.urlopen("http://forecast.weather.gov/product.php?site=NWS&issuedby=FWD&product=RR5&format=CI&version=1&glossary=0")
htmltext=htmlfile.read()
regex='<pre class="glossaryProduct">(.+?)</pre>'
pattern=re.compile(regex,re.S)
out=re.findall(pattern, htmltext)
text=str(out)
saveFile=open('test.txt', 'w')
saveFile.write(text)
saveFile.close()
print (text)
请提供您所编写的代码。如果您提供格式良好的示例,它也会很好。 – pratibha
进口的urllib 进口重新 HTMLFILE =了urllib.urlopen( “http://forecast.weather.gov/product.php?site=NWS&issuedby=FWD&product=RR5&format=CI&version=1&glossary=0”) 的htmlText = htmlfile.read( ) 正则表达式= '
' 图案= re.compile(正则表达式,re.S) 出= re.findall(图案,的htmlText) 文本= STR(下) saveFile的开放=(' test.txt的',' W') saveFile.write(文本) saveFile.close() 打印(文本) – Behi格式列表输出为好。 – pratibha