for temperature in dls:
temperature_pattern = re.compile('<ddclass="txt2">(.*?)</dd>')
temperature_dd = re.findall(temperature_pattern, temperature)
# print(temperature_dd)
#获取最低温对应的正则表达式
low_temperature_pattern =re.compile('^(*.?)~')
#^:匹配输入字符串的开始位置。 在源代码中~前的都是最低气温
low_li = re.findall(low_temperature_pattern, temperature_dd)
#利用low_temperature_pattern这个正则表达式,在temperature_dd中寻找,
#因为是从温度数据中找最低温和最高温
#我们希望处理的是第一个元素,所以是temperature_dd[0]
#获取最高温对应的正则表达式
high_temperature_pattern = re.compile('<b>(*.?)</b>')
high_li = re.findall(high_temperature_pattern, temperature_dd)
提示错误
错误说是没有可以重复到的,先打印了更往上的“temperature_dd”发现没有问题,那么问题就出在了正则表达式上。
根据上一条打印测试的数据发现开头结尾没有问题。
问题出在了表达式:
(*.?) #错误的
(.*?) #正确的
在正则表达式中:
#三个字符分别是这样的含义
. #匹配除“\n”之外的任何单个字符。
* #匹配前面的子表达式零次或多次。
? #当该字符紧跟在任何一个其他限制符(*,+,?,{n},{n,},{n,m})后面时,匹配模式是非贪婪的。--->
#--->非贪婪模式尽可能少的匹配所搜索的字,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。
修改之后,数据提取正确。