鱼C论坛

 找回密码
 立即注册
查看: 1480|回复: 12

关于正则表达式求助,谢谢

[复制链接]
发表于 2019-5-6 21:53:37 | 显示全部楼层 |阅读模式
30鱼币
html如下:
html = '''<tr class="" onmousemove="cursorOver(this)" onmouseout="cursorOut(this)" onclick="selectResult('4512')">
                    <td style="width:70px;" align="left">河西</td>
    <td style="width:80px;" align="left">公告</td>
    <td style="width:280px;" align="left">
            <a href="#this">监控类型</a>
    </td>
                    <td style="width:100px" align="left">2018-4-1</td>
  </tr>


<tr class="jtgs_table_td_bg" onmousemove="cursorOver(this)" onmouseout="cursorOut(this)" onclick="selectResult('4514')">
                    <td style="width:70px;" align="left">中心</td>
    <td style="width:80px;" align="left">公告</td>
    <td style="width:280px;" align="left">
            <a href="#this" title="比选类型">监控数据模型...</a>
    </td>
                    <td style="width:100px" align="left">2018-4-3</td>
  </tr>
'''
自己写的正则如下:
pattern = re.compile(r'\</tr\>.*?cursorOver\(this\)\".*?selectResult\(\'(\d+)\'\)"\>.*?width:70px.*?left"\>(.*?)\</td\>'
                         +'.*?width:80px.*?left"\>(.*?)\</td\>'
                        ,re.S)
   
items = re.findall(pattern,html)
print(items)

目前只匹配了一半,匹配结果为[('4512', '河西', '公告'), ('4514', '中心', '公告')]
个人想匹配后提取结果如下[('4512', '河西', '公告',监控类型,2018-4-1), ('4514', '中心', '公告',比选类型,2018-4-3)]
求各位大神能否补充下半部分,主要有两种情况,实在判断不好,需要全匹配后提取;感谢

最佳答案

查看完整内容

凑活着用吧
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-6 21:53:38 | 显示全部楼层
凑活着用吧
  1. # py3.7  pycharm

  2. import re
  3. html = '''<tr class="" onmousemove="cursorOver(this)" onmouseout="cursorOut(this)" onclick="selectResult('4512')">
  4.                     <td style="width:70px;" align="left">河西</td>
  5.     <td style="width:80px;" align="left">公告</td>
  6.     <td style="width:280px;" align="left">
  7.             <a href="#this">监控类型</a>
  8.     </td>
  9.                     <td style="width:100px" align="left">2018-4-1</td>
  10.   </tr>

  11. <tr class="jtgs_table_td_bg" onmousemove="cursorOver(this)" onmouseout="cursorOut(this)" onclick="selectResult('4514')">
  12.                     <td style="width:70px;" align="left">中心</td>
  13.     <td style="width:80px;" align="left">公告</td>
  14.     <td style="width:280px;" align="left">
  15.             <a href="#this" title="比选类型">监控数据模型...</a>
  16.     </td>
  17.                     <td style="width:100px" align="left">2018-4-3</td>
  18.   </tr>
  19. '''
  20. results= re.findall(r''''(\d+)'.*?left">(.*?)</td>.*?left">(.*?)</td>.*?((?<=this">)(.*?)(?=<)|(?<=title=")(.*?)(?=")).*?left">(.*?)</td>''',  
  21.                     html,re.M|re.S)
  22. if results:
  23.     for result in results:
  24.         print([i.strip() for i in result if len(i)])

  25. #  ['4512', '河西', '公告', '监控类型', '监控类型', '2018-4-1']
  26. #  ['4514', '中心', '公告', '比选类型', '比选类型', '2018-4-3']
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-6 23:14:58 | 显示全部楼层
r'\</tr\>.  前面都用r了,就不需要转义了,
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-7 00:08:41 | 显示全部楼层
  1. # coding: utf-8
  2. import requests
  3. from lxml import etree


  4. if __name__ == '__main__':
  5.   html='''<tr class="" onmousemove="cursorOver(this)" onmouseout="cursorOut(this)" onclick="selectResult('4512')">
  6.                      <td style="width:70px;" align="left">河西</td>
  7.      <td style="width:80px;" align="left">公告</td>
  8.      <td style="width:280px;" align="left">
  9.              <a href="#this">监控类型</a>
  10.      </td>
  11.                      <td style="width:100px" align="left">2018-4-1</td>
  12.    </tr>


  13. <tr class="jtgs_table_td_bg" onmousemove="cursorOver(this)" onmouseout="cursorOut(this)" onclick="selectResult('4514')">
  14.                      <td style="width:70px;" align="left">中心</td>
  15.      <td style="width:80px;" align="left">公告</td>
  16.      <td style="width:280px;" align="left">
  17.              <a href="#this" title="比选类型">监控数据模型...</a>
  18.      </td>
  19.                      <td style="width:100px" align="left">2018-4-3</td>
  20.    </tr>
  21.    '''
  22.   response = etree.HTML(html)
  23.   ls=[]
  24.   for x in range(2):
  25.     xd={}
  26.     xd["xm1"]= response.xpath("//tr/@onclick")[x][-6:-2]
  27.     xd["xm2"]= response.xpath("//tr/td[1]")[x].text
  28.     xd["xm3"]= response.xpath("//tr/td[2]")[x].text
  29.     if x==1:
  30.       xd["xm4"]= response.xpath("//tr/td[3]/a/@title")[0]
  31.     else:
  32.       xd["xm4"]= response.xpath("//tr/td[3]/a")[x].text
  33.     xd["xm5"]= response.xpath("//tr/td[4]")[x].text
  34.     ls.append(xd)
  35.   for i in ls:
  36.     print(i)   


  37. '''
  38. e:\>python ex8.py
  39. {'xm1': '4512', 'xm2': '河西', 'xm3': '公告', 'xm4': '监控类型', 'xm5': '2018-4-1'}
  40. {'xm1': '4514', 'xm2': '中心', 'xm3': '公告', 'xm4': '比选类型', 'xm5': '2018-4-3'}

  41. '''        
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-7 05:51:11 From FishC Mobile | 显示全部楼层
你的html太少,直接贴出网址得了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-7 20:04:38 From FishC Mobile | 显示全部楼层
kaohsing 发表于 2019-5-7 05:51
你的html太少,直接贴出网址得了。

((?<=this">)(.*?)(?=<)|(?<=title=")(.*?)(?="))
已经可以用了,就是这部分没有看懂,?<=    查了查也没搞懂
来自: 微社区
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-7 20:06:07 From FishC Mobile | 显示全部楼层
kaohsing 发表于 2019-5-7 05:51
你的html太少,直接贴出网址得了。

还需要求助,感谢
来自: 微社区
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-7 20:59:27 From FishC Mobile | 显示全部楼层
xue11 发表于 2019-5-7 20:04
((?

搜搜正则环视,就清楚了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-8 12:57:09 From FishC Mobile | 显示全部楼层
kaohsing 发表于 2019-5-7 20:59
搜搜正则环视,就清楚了。

求助正则表达式文档
来自: 微社区
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-8 13:45:40 From FishC Mobile | 显示全部楼层
百度不到去谷歌。

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-8 13:46:31 From FishC Mobile | 显示全部楼层
有的叫环视有的叫断言。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-18 23:17:57 | 显示全部楼层

为什么有空格,重复数据,比如 必选类型。我将环视学习,感觉你这完全中正确啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-19 07:11:04 | 显示全部楼层
  1. # py3.7  pycharm

  2. import re
  3. html = '''<tr class="" onmousemove="cursorOver(this)" onmouseout="cursorOut(this)" onclick="selectResult('4512')">
  4.                 <td style="width:70px;" align="left">河西</td>
  5.                 <td style="width:80px;" align="left">公告</td>
  6.                 <td style="width:280px;" align="left">            <a href="#this">监控类型</a>    </td>
  7.                 <td style="width:100px" align="left">2018-4-1</td>
  8.         </tr>
  9.          
  10.         <tr class="jtgs_table_td_bg" onmousemove="cursorOver(this)" onmouseout="cursorOut(this)" onclick="selectResult('4514')">
  11.             <td style="width:70px;" align="left">中心</td>
  12.             <td style="width:80px;" align="left">公告</td>
  13.             <td style="width:280px;" align="left">  <a href="#this" title="比选类型">监控数据模型...</a>            </td>
  14.             <td style="width:100px" align="left">2018-4-3</td>
  15.         </tr>
  16. '''
  17. results= re.findall(r''''(\d+)'.*?left">(.*?)</td>.*?left">(.*?)</td>.*?this">(.*?)</a>.*?left">([\d\-]+)</td>|'(\d+)'.*?left">(.*?)</td>.*?left">(.*?)</td>.*?title="(.*?)".*?left">([\d\-]+)</td>''', html,
  18.         re.M | re.S)
  19. if results:
  20.     for result in results:
  21.         print([i for i in result if len(i)])

  22. #  ['4512', '河西', '公告', '监控类型', '2018-4-1']
  23. #  ['4514', '中心', '公告', '比选类型', '2018-4-3']
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-15 17:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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