Python正则表达式区分并使用字符串中的重复模式

Python正则表达式区分并使用字符串中的重复模式,python,html,regex,string,Python,Html,Regex,String,编辑:因此,在下面答案的帮助下,我意识到用正则表达式解析html通常是个坏主意。值得一提的是,如果有一天其他人看到我的帖子时提出了同样的问题,这里有一个链接,指向关于这个主题的两个类似问题,你可能会发现更多的辩论和解释是有用的:这一个: 规范:Python 3.3.1 我想做什么:我在写一个网页提取器,从一个网站上获取天气数据,对于我的项目来说,它有三个有意义的部分:温度“现在”、“今天早些时候”和“今晚”。我打算只抓取这3个数字,而忽略所有其他文本。在下面的代码中,我使用温度数字前面的特定ht

编辑:因此,在下面答案的帮助下,我意识到用正则表达式解析html通常是个坏主意。值得一提的是,如果有一天其他人看到我的帖子时提出了同样的问题,这里有一个链接,指向关于这个主题的两个类似问题,你可能会发现更多的辩论和解释是有用的:这一个:

规范:Python 3.3.1

我想做什么:我在写一个网页提取器,从一个网站上获取天气数据,对于我的项目来说,它有三个有意义的部分:温度“现在”、“今天早些时候”和“今晚”。我打算只抓取这3个数字,而忽略所有其他文本。在下面的代码中,我使用温度数字前面的特定html元素作为模式来帮助我获取数字本身

我需要的所有数据都在这段html代码摘录中:(即
89
96
80


今天早些时候
今晚
89度;F
感觉像
94度;
96度;
下午4点45分高
80度;
低
我想出的解决方案:

import urllib.request
import re

# open the webpage and read the html code into a string; 
base = urllib.request.urlopen('http://www.weather.com/weather/today/Washington+DC+USDC0001:1:US')
f = base.readlines()
f = str(f)


# temperature "Right Now" 
match1 = re.search(r'<div class="wx-temperature"><span itemprop="temperature-fahrenheit">\w\w',f)

if match1:
    result1 = match1.group()
    right_now = result1[68:]
    print(right_now)


# temperature "Earlier Today"
match2 = re.search(r'<div class="wx-temperature">\w\w',f)

if match2:
    result2 = match2.group()
    ealier_today = result2[28:]
    print(ealier_today)


# temperature "Tonight"
match3 = re.search(r'<div class="wx-temperature">\w\w',f)

if match3:
    result3 = match3.group()
    tonight = result3[28:]
    print(tonight)
导入urllib.request
进口稀土
#打开网页,将html代码读入字符串;
base=urllib.request.urlopen('http://www.weather.com/weather/today/Washington+DC+USDC0001:1:US')
f=base.readlines()
f=str(f)
#“现在”的温度
match1=重新搜索(r'\w\w',f)
如果匹配1:
result1=match1.group()
right_now=result1[68:]
打印(立即打印)
#“今天早些时候”的气温
match2=重新搜索(r'\w\w',f)
如果匹配2:
result2=match2.group()
ealier_today=result2[28:]
打印(今天上午)
#“今晚”的温度
match3=重新搜索(r'\w\w',f)
如果匹配3:
result3=match3.group()
今晚=结果3[28:]
打印(今晚)
这三条print语句仅用于测试数据是否被正确捕获


我的问题:问题出现在第三个正则表达式(
match3
)上,显示了
match2
的温度。我想这是因为它使用与第二个相同的正则表达式模式?所以我想我的问题是,如何用相同的正则表达式模式搜索多个结果。还是你只能抓住第一个出现的模式?我对Python非常陌生,这是我进入正则表达式的头几天。如果您能分享一些关于我的解决方案或我对这个项目的总体思路的一般性建议,我将不胜感激。谢谢大家!

也许我误解了你的问题,但你只是在寻找答案


如果天气降到一位数或升到三位数,这种方法会给你一些灵活性。我添加了
-?
,这意味着字符
-
将出现0或1次,以防遇到负临时值。

请不要使用正则表达式解析html。谢谢您的回答!我没有意识到用正则表达式解析html是个坏主意。使用BeautifulSoup的回答很好。谢谢当然。我希望你能像我一样发现BeautifulSoup很有帮助。
import urllib.request
import re

# open the webpage and read the html code into a string; 
base = urllib.request.urlopen('http://www.weather.com/weather/today/Washington+DC+USDC0001:1:US')
f = base.readlines()
f = str(f)


# temperature "Right Now" 
match1 = re.search(r'<div class="wx-temperature"><span itemprop="temperature-fahrenheit">\w\w',f)

if match1:
    result1 = match1.group()
    right_now = result1[68:]
    print(right_now)


# temperature "Earlier Today"
match2 = re.search(r'<div class="wx-temperature">\w\w',f)

if match2:
    result2 = match2.group()
    ealier_today = result2[28:]
    print(ealier_today)


# temperature "Tonight"
match3 = re.search(r'<div class="wx-temperature">\w\w',f)

if match3:
    result3 = match3.group()
    tonight = result3[28:]
    print(tonight)
match3 = re.findall(r'<div class="wx-temperature">\w\w',f)
>>> from bs4 import BeautifulSoup
>>> html = '''<div class="wx-timepart-title">
Earlier Today
</div>
<div class="wx-timepart-title">Tonight</div>
<div class="wx-data-part wx-first">
<img src="http://s.imwx.com/v.20120328.084208/img/wxicon/120/29.png" height="120" width="120" alt="Partly Cloudy" class="wx-weather-icon">
</div>
<div class="wx-data-part">
<img src="http://s.imwx.com/v.20120328.084208/img/wxicon/120/30.png" height="120" width="120" alt="Partly Cloudy" class="wx-weather-icon">
</div>
<div class="wx-data-part">
<img src="http://s.imwx.com/v.20120328.084208/img/wxicon/120/29.png" height="120" width="120" alt="Partly Cloudy" class="wx-weather-icon">
</div>
<div class="wx-data-part wx-first">
<div class="wx-temperature"><span itemprop="temperature-fahrenheit">89</span><span class="wx-degrees">&deg;<span class="wx-unit">F</span></span></div>
<div class="wx-temperature-label">FEELS LIKE
<span itemprop="feels-like-temperature-fahrenheit">94</span>&deg;</div>
</div>
<div class="wx-data-part">
<div class="wx-temperature">96<span class="wx-degrees">&deg;</span></div>
<div class="wx-temperature-label">HIGH AT 4:45 PM</div>
</div>
<div class="wx-data-part">
<div class="wx-temperature">80<span class="wx-degrees">&deg;</span></div>
<div class="wx-temperature-label">LOW</div>
</div>  '''
>>> soup = BeautifulSoup(html)
>>> for temp in soup.find_all(class_="wx-temperature"):
    print(temp.text)       # or add these to a list or make a list comprehension


89°F
96°
80°
>>> import re
>>> for temp in soup.find_all(class_="wx-temperature"):
    print(re.match(r'-?\d+', temp.text).group())


89
96
80