star0angel 发表于 2024-4-18 10:01

刚学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℃']

superTian 发表于 2024-4-18 10:06

pattern = r'</span><span class="wd">(.+?)<span class="wdTime">'

漁滒 发表于 2024-4-18 10:06

默认使用的是贪婪匹配,即单次结果匹配尽可能多的字符串

star0angel 发表于 2024-4-18 10:07

superTian 发表于 2024-4-18 10:06
pattern = r'(.+?)'

.*不是匹配所有符合条件的吗

superTian 发表于 2024-4-18 10:07

star0angel 发表于 2024-4-18 10:07
.*不是匹配所有符合条件的吗

楼上正解

star0angel 发表于 2024-4-18 10:16

漁滒 发表于 2024-4-18 10:06
默认使用的是贪婪匹配,即单次结果匹配尽可能多的字符串

怎么改成不是贪婪匹配

milu1123 发表于 2024-4-18 10:45

正则会用的就会用,不会用的就是不会用。改用其他把,BS4,xpath个人觉得比正则简单好用

xglovecode 发表于 2024-4-18 11:19

可以去看下正则

weiya909 发表于 2024-4-18 11:26

要不要从新排一下版

无知灰灰 发表于 2024-4-18 11:26

不一定要正则吧xpath或beautifulsoup之类的去取。。
页: [1] 2 3
查看完整版本: 刚学python请教