吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1019|回复: 23
收起左侧

[求助] 刚学python请教

[复制链接]
star0angel 发表于 2024-4-18 10:01
本帖最后由 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℃']

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
junjia215 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

superTian 发表于 2024-4-18 10:06
pattern = r'</span><span class="wd">(.+?)<span class="wdTime">'

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
star0angel + 1 + 1 谢谢@Thanks!

查看全部评分

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

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
star0angel + 1 + 1 谢谢@Thanks!

查看全部评分

 楼主| star0angel 发表于 2024-4-18 10:07
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之类的去取。。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 15:03

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表