鱼C论坛

 找回密码
 立即注册
查看: 3048|回复: 18

多个同样的标签,怎么写 正则表达式?

[复制链接]
发表于 2017-9-28 00:11:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

在分析一个站点的数据时,
突然就蒙了。。

有些标签相同的呀。怎么写呢?

我的例子:


网页的某段源码:

  1. <td class="tdbbs tdbrs">2017-09-01</td>
  2.     <td class="tdbbs tdbrs">637564</td>
  3.     <td class="tdbb tdbrs" align="center">
  4.         <table width="300" border="0" cellpadding="0" cellspacing="0" class="m0a">
  5.           <tr>
  6.           <td><span class="Bwi dB8x"></span></td>
  7.                   <td><span class="Bwi dB6x"></span></td>
  8.                   <td><span class="Bwi dB2x"></span></td>
  9.                   <td><span class="Bwi dB1x"></span></td>
  10.                   <td><span class="Bwi dB7x"></span></td>
  11.                   <td><span class="Bwi dB5x"></span></td>
  12.                   <td><span class="Bwi dB10x"></span></td>
  13.                   <td><span class="Bwi dB3x"></span></td>
  14.                   <td><span class="Bwi dB9x"></span></td>
  15.                   <td><span class="Bwi dB4x"></span></td>
  16.                   </tr>
  17.         </table>
  18.         </td>
  19.        
  20.        
  21. <td class="tdbbs tdbrs">2017-09-01</td>
  22.     <td class="tdbbs tdbrs">637563</td>
  23.     <td class="tdbb tdbrs" align="center">
  24.         <table width="300" border="0" cellpadding="0" cellspacing="0" class="m0a">
  25.           <tr>
  26.           <td><span class="Bwi dB10x"></span></td>
  27.                   <td><span class="Bwi dB2x"></span></td>
  28.                   <td><span class="Bwi dB1x"></span></td>
  29.                   <td><span class="Bwi dB8x"></span></td>
  30.                   <td><span class="Bwi dB4x"></span></td>
  31.                   <td><span class="Bwi dB9x"></span></td>
  32.                   <td><span class="Bwi dB3x"></span></td>
  33.                   <td><span class="Bwi dB7x"></span></td>
  34.                   <td><span class="Bwi dB6x"></span></td>
  35.                   <td><span class="Bwi dB5x"></span></td>
  36.                   </tr>
  37.         </table>
复制代码



我要获取    <td class="tdbbs tdbrs">637564</td> 中的637564

我这样写 正则表达式:pattern = re.compile(r'<td.*?class="tdbbs tdbrs">(.*?)</td>', re.S)

最后结果把,前面的 这段代码里的<td class="tdbbs tdbrs">2017-09-01</td> 2017-09-01 也提取出来了。

如何才能更准确的定位到第二段相同的标签?


因为需要,我又单独写了,另外的一个正则表达式:
pattern = re.compile(r'<span.*?class="Bwi.*?dB(.*?)x"></span>', re.S)

目的是想把这段标签里的 数字 提取出来
                  <td><span class="Bwi dB8x"></span></td>
                  <td><span class="Bwi dB6x"></span></td>
                  <td><span class="Bwi dB2x"></span></td>
                  <td><span class="Bwi dB1x"></span></td>
                  <td><span class="Bwi dB7x"></span></td>
                  <td><span class="Bwi dB5x"></span></td>
                  <td><span class="Bwi dB10x"></span></td>
                  <td><span class="Bwi dB3x"></span></td>
                  <td><span class="Bwi dB9x"></span></td>
                  <td><span class="Bwi dB4x"></span></td>

即:8 6 2 1 7 5 10 3 9 4

但也没能如愿

这个正则表达式 要如何写呢?

各位前辈  请指点迷津


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

使用道具 举报

发表于 2017-9-28 00:13:11 | 显示全部楼层
用beautifulsoup 找子标签
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-28 09:32:27 | 显示全部楼层
来一个很low的写法 直接硬拼:

re.findall('<td><span\s*class="Bwi\s*dB(\d+)x">', a, re.I | re.M | re.S)

数字就全出来了。
['8',
'6',
'2',
'1',
'7',
'5',
'10',
'3',
'9',
'4',
'10',
'2',
'1',
'8',
'4',
'9',
'3',
'7',
'6',
'5']
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-28 15:14:28 | 显示全部楼层
、尾指一勾 发表于 2017-9-28 09:32
来一个很low的写法 直接硬拼:

re.findall('', a, re.I | re.M | re.S)



非常感谢分享

我的那段 正则 也能 提取出 所有的 数字。
但 我想把  这两条正则表达式的结果 合并到一起。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-28 15:36:21 | 显示全部楼层

合并?怎么合并?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-28 15:47:08 | 显示全部楼层


我想要的 最后 结果是:

637564    8 6 2 1 7 5 10 3 9 4
637563    10 2 1 8 4 9 3 7 6 5

即 分别保留:这组标签的 数字串 <td class="tdbbs tdbrs">637564</td>

把这段标签里的 数字 提取出来
                  <td><span class="Bwi dB8x"></span></td>
                  <td><span class="Bwi dB6x"></span></td>
                  <td><span class="Bwi dB2x"></span></td>
                  <td><span class="Bwi dB1x"></span></td>
                  <td><span class="Bwi dB7x"></span></td>
                  <td><span class="Bwi dB5x"></span></td>
                  <td><span class="Bwi dB10x"></span></td>
                  <td><span class="Bwi dB3x"></span></td>
                  <td><span class="Bwi dB9x"></span></td>
                  <td><span class="Bwi dB4x"></span></td>

最后合并在一起
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-28 16:16:31 | 显示全部楼层
要学习 发表于 2017-9-28 15:47
我想要的 最后 结果是:

637564    8 6 2 1 7 5 10 3 9 4
  1. html = '''
  2. <td class="tdbbs tdbrs">2017-09-01</td>
  3. &nbsp; &nbsp; <td class="tdbbs tdbrs">637564</td>
  4. &nbsp; &nbsp; <td class="tdbb tdbrs" align="center">
  5. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<table width="300" border="0" cellpadding="0" cellspacing="0" class="m0a">
  6. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <tr>
  7. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <td><span class="Bwi dB8x"></span></td>
  8. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB6x"></span></td>
  9. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB2x"></span></td>
  10. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB1x"></span></td>
  11. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB7x"></span></td>
  12. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB5x"></span></td>
  13. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB10x"></span></td>
  14. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB3x"></span></td>
  15. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB9x"></span></td>
  16. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB4x"></span></td>
  17. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;</tr>
  18. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</table>
  19. &nbsp; &nbsp; &nbsp; &nbsp; </td>
  20. &nbsp; &nbsp; &nbsp; &nbsp;
  21. &nbsp; &nbsp; &nbsp; &nbsp;
  22. <td class="tdbbs tdbrs">2017-09-01</td>
  23. &nbsp; &nbsp; <td class="tdbbs tdbrs">637563</td>
  24. &nbsp; &nbsp; <td class="tdbb tdbrs" align="center">
  25. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<table width="300" border="0" cellpadding="0" cellspacing="0" class="m0a">
  26. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <tr>
  27. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <td><span class="Bwi dB10x"></span></td>
  28. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB2x"></span></td>
  29. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB1x"></span></td>
  30. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB8x"></span></td>
  31. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB4x"></span></td>
  32. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB9x"></span></td>
  33. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB3x"></span></td>
  34. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB7x"></span></td>
  35. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB6x"></span></td>
  36. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<td><span class="Bwi dB5x"></span></td>
  37. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;</tr>
  38. &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;</table>'''

  39. import re
  40. titles=re.findall('<td class="tdbbs tdbrs">(\d+?)</td>', html)
  41. contents = re.findall('<td><span class="Bwi dB(\d+)x"></span></td>',html)
  42. for t in range(len(titles)):
  43.         print(titles[t])
  44.         print(contents[10*t:10*t+10])
复制代码

637564
['8', '6', '2', '1', '7', '5', '10', '3', '9', '4']
637563
['10', '2', '1', '8', '4', '9', '3', '7', '6', '5']
[Finished in 0.1s]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-28 16:28:09 | 显示全部楼层


测试中的结果。。。

也不对。。 哎、、、

  1. qh = [line[0:6] for line in m_td]
  2. print qh
复制代码



得到的结果:

  1. ['2017-0', '637564', '2017-0', '637563'
复制代码


看来这样写 也不行。。我是想取  第二组数据呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-28 16:28:39 | 显示全部楼层
jerryxjr1220 发表于 2017-9-28 16:16
637564
['8', '6', '2', '1', '7', '5', '10', '3', '9', '4']
637563

先谢谢版主,,,

我再测试测试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-28 20:37:47 | 显示全部楼层
jerryxjr1220 发表于 2017-9-28 16:16
637564
['8', '6', '2', '1', '7', '5', '10', '3', '9', '4']
637563

晚上好。。

如你的例子里。。

  1. print(titles[t])
  2.         print(contents[10*t:10*t+10])
复制代码


最后结果如何写成这样的格式:
637564    8 6 2 1 7 5 10 3 9 4
637563    10 2 1 8 4 9 3 7 6 5
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-28 20:40:03 | 显示全部楼层
我现在的问题是。。

我搞成了 两个变量。。

两个变量 要一一对应,这个如何写呢?


例子:

变量一为:

637564
637563

变量二为:
8 6 2 1 7 5 10 3 9 4
10 2 1 8 4 9 3 7 6 5

最后我要如何写 才能 使得结果为:
637564    8 6 2 1 7 5 10 3 9 4
637563    10 2 1 8 4 9 3 7 6 5
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-28 21:25:46 | 显示全部楼层
头大了。。。

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

使用道具 举报

 楼主| 发表于 2017-9-28 22:24:01 | 显示全部楼层
jerryxjr1220 发表于 2017-9-28 16:16
637564
['8', '6', '2', '1', '7', '5', '10', '3', '9', '4']
637563

版主  在吗?

像这样的 字符串。。
怎么写 正则。才能搜 出来  数字

  1. <td class="tdbbs tdbrs">
  2.     <b class="of">7&nbsp;</b><b>4&nbsp;</b><b>3&nbsp;</b><b>1&nbsp;</b><b>6&nbsp;</b><b>9&nbsp;</b><b>10&nbsp;</b><b>5&nbsp;</b><b>8&nbsp;</b><b>2&nbsp;</b></td>
复制代码



7&nbsp;   4&nbsp;  3&nbsp;

我只需要 数字
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-29 21:59:34 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-29 22:04:42 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-29 22:11:29 | 显示全部楼层
为啥只要那个数字,不要里面的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-30 00:29:05 | 显示全部楼层
新手·ing 发表于 2017-9-29 22:11
为啥只要那个数字,不要里面的

版主好。

这 &nbsp; 是空格啊,不要空格。。只要 数字 即可。这段代码里 有10个数字,1-10

  1. <td class="tdbbs tdbrs">
  2.     <b class="of">7&nbsp;</b><b>4&nbsp;</b><b>3&nbsp;</b><b>1&nbsp;</b><b>6&nbsp;</b><b>9&nbsp;</b><b>10&nbsp;</b><b>5&nbsp;</b><b>8&nbsp;</b><b>2&nbsp;</b></td>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-30 01:15:41 | 显示全部楼层
新手·ing 发表于 2017-9-29 22:11
为啥只要那个数字,不要里面的




版主好。。。

我现在 调试到的结果 为 这样的。

  1. 1&nbsp;8&nbsp;9&nbsp;7&nbsp;6&nbsp;5&nbsp;4&nbsp;2&nbsp;3&nbsp;10&nbsp;
复制代码



我如何 删除

我用这个,只能删除 最后一个的 &nbsp;


    list4=list3.strip('&nbsp;')

是不是要写循环,这样的循环又要如何写?

一次要删除10个&nbsp;
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-30 21:39:37 | 显示全部楼层
要学习 发表于 2017-9-30 01:15
版主好。。。

我现在 调试到的结果 为 这样的。

找Jerry
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-24 03:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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