刚学python请教
本帖最后由 star0angel 于 2024-4-18 10:05 编辑text = <li class="pi"><span class="sort">排名</span><span class="city">城市</span><span class="prov">所属省</span><span class="lastTemp">最高气温(出现时间)</span></li>
<li class="on"><span class="ord"><i>1</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310201.shtml" target="_blank">三亚</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">33℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>2</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310214.shtml" target="_blank">保亭</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">33℃<span class="wdTime">(13时)</span></span></li><li><span class="ord"><i>3</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310215.shtml" target="_blank">万宁</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">30℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>4</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310222.shtml" target="_blank">五指山</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">30℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>5</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310206.shtml" target="_blank">昌江</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">29℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>6</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101290305.shtml" target="_blank">元阳</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/yunnan.shtml" target="_blank">云南</a></span><span class="wd">27℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>7</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101290709.shtml" target="_blank">元江</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/yunnan.shtml" target="_blank">云南</a></span><span class="wd">27℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>8</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310202.shtml" target="_blank">东方</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">27℃<span class="wdTime">(10时)</span></span></li><li><span class="ord"><i>9</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310208.shtml" target="_blank">琼中</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">27℃<span class="wdTime">(10时)</span></span></li><li><span class="ord"><i>10</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310211.shtml" target="_blank">琼海</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">27℃<span class="wdTime">(12时)</span></span></li>
pattern = r'</span><span class="wd">(.*)<span class="wdTime">'
wd = re.findall(pattern , text)
匹配结果为啥不止是温度要怎么匹配所有的温度
['33℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>2</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310214.shtml" target="_blank">保亭</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">33℃<span class="wdTime">(13时)</span></span></li><li><span class="ord"><i>3</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310215.shtml" target="_blank">万宁</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">30℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>4</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310222.shtml" target="_blank">五指山</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">30℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>5</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310206.shtml" target="_blank">昌江</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">29℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>6</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101290305.shtml" target="_blank">元阳</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/yunnan.shtml" target="_blank">云南</a></span><span class="wd">27℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>7</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101290709.shtml" target="_blank">元江</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/yunnan.shtml" target="_blank">云南</a></span><span class="wd">27℃<span class="wdTime">(12时)</span></span></li><li><span class="ord"><i>8</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310202.shtml" target="_blank">东方</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">27℃<span class="wdTime">(10时)</span></span></li><li><span class="ord"><i>9</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310208.shtml" target="_blank">琼中</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">27℃<span class="wdTime">(10时)</span></span></li><li><span class="ord"><i>10</i></span><span class="city"><a href="http://www.weather.com.cn/weather1d/101310211.shtml" target="_blank">琼海</a></span><span class="prov"><a href="http://www.weather.com.cn/html/province/hainan.shtml" target="_blank">海南</a></span><span class="wd">27℃'] pattern = r'</span><span class="wd">(.+?)<span class="wdTime">' 默认使用的是贪婪匹配,即单次结果匹配尽可能多的字符串 superTian 发表于 2024-4-18 10:06
pattern = r'(.+?)'
.*不是匹配所有符合条件的吗 star0angel 发表于 2024-4-18 10:07
.*不是匹配所有符合条件的吗
楼上正解 漁滒 发表于 2024-4-18 10:06
默认使用的是贪婪匹配,即单次结果匹配尽可能多的字符串
怎么改成不是贪婪匹配 正则会用的就会用,不会用的就是不会用。改用其他把,BS4,xpath个人觉得比正则简单好用 可以去看下正则 要不要从新排一下版 不一定要正则吧xpath或beautifulsoup之类的去取。。