鱼C论坛

 找回密码
 立即注册
查看: 3106|回复: 22

[已解决]非贪婪.*?的bug?findall不能匹配出符合条件的记录,why?--已解决,正则不适合html

[复制链接]
发表于 2022-10-9 11:58:37 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 blackantt 于 2022-10-10 20:53 编辑

findall 一个文件,它只能匹配出前几条符合条件的记录并且还是错的。这是啥原因?

  最有可能的就是,   多个  .*?    这种模式用的不对  或者有bug?



out8.txt 附后,请看看

  1. import re

  2. fp = open('C:/Users/dengz/Downloads/out8.txt','r',encoding='utf-8')
  3. txt = fp.read()

  4. pattern = re.compile(r'<div class="css-em857x"><a title=".*? profile" href="(/profile/\d{6,8})">(.*?)</a></div></h4>.*?title="(.*?)".*?</span></div><div class="MuiBox-root css-15ro776"><span aria-hidden="true" title="China" .*?1 shared interest: Languages &amp; Cultures</p></div><div class=', re.M)

  5. items = re.findall(pattern, txt)
  6. print('本页的所有人', items)

  7. fp.close()
复制代码




需求是,从所有记录中把含有5部分的记录摘出来,具体是把符合后2个条件的前3个部分的内容取出来。  "profile/6-8位数字...名字...第一个国籍.....第二个国籍....1 share interest",  要把含有5段内容的数据的前3段取出来。红色的后2段是判定条件
pattern = re.compile(r'<div class="css-em857x"><a title=".*? profile" href="(/profile/\d{6,8})">(.*?)</a></div></h4>.*?title="(.*?)".*?</span></div><div class="MuiBox-root css-15ro776"><span aria-hidden="true" title="China" .*?1 shared interest: Languages &amp; Cultures</p></div><div class=', re.M)

第一次结果是:
页的所有人 [('/profile/9663344', 'Rui Yang', 'China'), ('/profile/9653506', 'Hanna Aschhoff', 'Germany'), ('/profile/9665784', 'Hou Ming', 'China'), ('/profile/9509995', 'Maria Gu', 'China'), ('/profile/9608580', 'Selina Li', 'China'), ('/profile/9574782', 'Alex L', 'Australia'), ('/profile/9598870', 'Han Na', 'China')]

其实后面还有,(至少还有一个  /profile/9575513  Ozlem Akgoz  Turkey)
它不搜了。

实际上,正确答案应该是,

本页的所有人 [('/profile/9509916', 'Tafadzwa Sylvester Mashayamombe', 'Zimbabwe', 'China', '1 shared interest'), ('/profile/9653506', 'Hanna Aschhoff', 'Germany', 'China', '1 shared interest'), ('/profile/9627110', 'Song Xue', 'United States of America', 'China', '1 shared interest'), ('/profile/9593679', 'Gowthm R', 'India', 'China', '1 shared interest'), ('/profile/9574782', 'Alex L', 'Australia', 'China', '1 shared interest'), ('/profile/9575513', 'Ozlem Akgoz', 'Turkey', 'China', '1 shared interest')]


out8.zip (31.09 KB, 下载次数: 8)
最佳答案
2022-10-9 16:26:07
blackantt 发表于 2022-10-9 16:23
好,我来学习 html
我觉得就是  .*?  的bug问题。

不是html 是lxml  PIP INSTALL LXML
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-9 12:05:11 From FishC Mobile | 显示全部楼层
你要检索神马数据,也许不用正则才更好
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-9 12:12:14 | 显示全部楼层
wp231957 发表于 2022-10-9 12:05
你要检索神马数据,也许不用正则才更好


第一次结果是:
页的所有人 [('/profile/9663344', 'Rui Yang', 'China'), ('/profile/9653506', 'Hanna Aschhoff', 'Germany'), ('/profile/9665784', 'Hou Ming', 'China'), ('/profile/9509995', 'Maria Gu', 'China'), ('/profile/9608580', 'Selina Li', 'China'), ('/profile/9574782', 'Alex L', 'Australia'), ('/profile/9598870', 'Han Na', 'China')]

其实后面还有,它不搜了。

帮我跑一跑,看看啥问题呗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-9 13:12:58 | 显示全部楼层
blackantt 发表于 2022-10-9 12:12
第一次结果是:
页的所有人 [('/profile/9663344', 'Rui Yang', 'China'), ('/profile/9653506', 'Han ...

你这是从哪个网页上 扒出来的吧  方便的话  把原网址发出来
肯定有比正则快速的检索方案
这源码太长了  而且很多无用数据 在里面混淆   看的眼睛都疼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-9 13:38:50 From FishC Mobile | 显示全部楼层
不用怎么看呀,就用已有的最后一条结果,先找最后一个 Han na,再往下找 profile。它只匹配到Han na,就不往下了。奇怪的是,把开头到Han na删掉,它就又能搜到下一个了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-9 13:41:51 | 显示全部楼层
blackantt 发表于 2022-10-9 12:12
第一次结果是:
页的所有人 [('/profile/9663344', 'Rui Yang', 'China'), ('/profile/9653506', 'Han ...

你看这个是不是你想要的结果
如果是的话,我会贴出代码
PS D:\wpp> & D:/Python37/python.exe d:/wpp/test4.py
{'profile': '/profile/9663344', 'text': 'Rui Yang', 'gj': 'China'}
{'profile': '/profile/9675226', 'text': 'zhang denise', 'gj': 'China'}
{'profile': '/profile/9648420', 'text': 'Wang Zixin', 'gj': 'China'}
{'profile': '/profile/9559287', 'text': 'Ye Tao', 'gj': 'China'}
{'profile': '/profile/9643008', 'text': 'Vanessa Fann', 'gj': 'China'}
{'profile': '/profile/9509916', 'text': 'Tafadzwa Sylvester Mashayamombe', 'gj': 'Zimbabwe'}
{'profile': '/profile/9653506', 'text': 'Hanna Aschhoff', 'gj': 'Germany'}
{'profile': '/profile/9665784', 'text': 'Hou Ming', 'gj': 'China'}
{'profile': '/profile/9635533', 'text': 'Eva Wang', 'gj': 'China'}
{'profile': '/profile/9534536', 'text': 'Cedar Chen', 'gj': 'China'}
{'profile': '/profile/9644829', 'text': 'Johnny Lei', 'gj': 'China'}
{'profile': '/profile/9598966', 'text': 'Shakira Han', 'gj': 'China'}
{'profile': '/profile/9533765', 'text': 'Ina Che', 'gj': 'China'}
{'profile': '/profile/9627110', 'text': 'Song Xue', 'gj': 'United States of America'}
{'profile': '/profile/9509995', 'text': 'Maria Gu', 'gj': 'China'}
{'profile': '/profile/9633471', 'text': '婷婷 王', 'gj': 'China'}
{'profile': '/profile/9575439', 'text': 'Jessie Feng', 'gj': 'China'}
{'profile': '/profile/9642985', 'text': 'Kiyono Sakashita', 'gj': 'China'}
{'profile': '/profile/9666052', 'text': 'Mario Ma', 'gj': 'China'}
{'profile': '/profile/9609828', 'text': 'Lea Chang', 'gj': 'China'}
{'profile': '/profile/9665784', 'text': 'Hou Ming', 'gj': 'China'}
{'profile': '/profile/9675226', 'text': 'zhang denise', 'gj': 'China'}
{'profile': '/profile/9524002', 'text': 'Wang phoebe', 'gj': 'China'}
{'profile': '/profile/9543657', 'text': 'Avatar Vanessa Chen', 'gj': 'China'}
{'profile': '/profile/9593679', 'text': 'Gowthm R', 'gj': 'India'}
{'profile': '/profile/9608580', 'text': 'Selina Li', 'gj': 'China'}
{'profile': '/profile/9618190', 'text': 'Jennifer Zhang', 'gj': 'China'}
{'profile': '/profile/9513552', 'text': 'Sarbast Mohammed', 'gj': 'Iraq'}
{'profile': '/profile/9598420', 'text': 'steve wu', 'gj': 'China'}
{'profile': '/profile/9574782', 'text': 'Alex L', 'gj': 'Australia'}
{'profile': '/profile/9598870', 'text': 'Han Na', 'gj': 'China'}
{'profile': '/profile/9574777', 'text': 'Jessica Han', 'gj': 'China'}
{'profile': '/profile/9578991', 'text': 'Lucia Gómez Lobato', 'gj': 'Spain'}
{'profile': '/profile/9655857', 'text': 'Jane J', 'gj': 'China'}
{'profile': '/profile/9533894', 'text': 'Alberto Conejo', 'gj': 'Mexico'}
{'profile': '/profile/9614007', 'text': 'Mood SEIDOU', 'gj': 'Togo'}
{'profile': '/profile/9575513', 'text': 'Ozlem Akgoz', 'gj': 'Turkey'}
{'profile': '/profile/9535299', 'text': 'Hamza Raja', 'gj': 'United States of America'}
{'profile': '/profile/9491991', 'text': 'David So', 'gj': 'Australia'}
{'profile': '/profile/9638011', 'text': 'Charles Liu', 'gj': 'China'}
PS D:\wpp>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-9 13:57:22 | 显示全部楼层
blackantt 发表于 2022-10-9 12:12
第一次结果是:
页的所有人 [('/profile/9663344', 'Rui Yang', 'China'), ('/profile/9653506', 'Han ...

去掉后面的“尾巴”,可以匹配到40个,看看是不是都是你想要的,以及是否匹配完了。
  1. with open('out8.txt', encoding='utf8') as f:
  2.     results = re.findall(r'<div class="css-em857x"><a title=".*? profile" href="(/profile/\d{6,8})">(.*?)</a></div></h4>.*?title="(.*?)"', f.read())
  3.     print(results)
  4.     print(len(results))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-9 14:00:09 | 显示全部楼层
阿奇_o 发表于 2022-10-9 13:57
去掉后面的“尾巴”,可以匹配到40个,看看是不是都是你想要的,以及是否匹配完了。

嗯,我也检索到40条记录,不过我始终认为  静态解析  正则永远都是最后一个要考虑的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-9 14:03:36 | 显示全部楼层
wp231957 发表于 2022-10-9 13:41
你看这个是不是你想要的结果
如果是的话,我会贴出代码
PS D:\wpp> & D:/Python37/python.exe d:/wpp/t ...

非常好,就是这个。 请贴出你的正则。  那我的为啥只能匹配一部分呢?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-9 14:06:04 | 显示全部楼层
blackantt 发表于 2022-10-9 14:03
非常好,就是这个。 请贴出你的正则。  那我的为啥只能匹配一部分呢?

正则你看7楼吧
我的不是正则:
  1. import requests
  2. from lxml import etree

  3. parse = etree.HTMLParser(encoding="UTF-8")
  4. html2 = etree.parse('out8.html',parser=parse)   #读取文件方式解析
  5. html2=html2.xpath("//div[@class='MuiBox-root css-d0uhtl']")
  6. for x in range(len(html2)):
  7.     #print(etree.tostring(html2[x],encoding="utf-8" ,pretty_print=True).decode("utf-8"))
  8.     data={}
  9.     data["profile"]=html2[x].xpath('./h4/div/a/@href')[0]
  10.     data["text"]=html2[x].xpath('./h4/div/a/text()')[0]
  11.     data["gj"]=html2[x].xpath('./div/div/span/@title')[0]
  12.     print(data)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-9 14:42:29 | 显示全部楼层
本帖最后由 blackantt 于 2022-10-9 14:56 编辑
阿奇_o 发表于 2022-10-9 13:57
去掉后面的“尾巴”,可以匹配到40个,看看是不是都是你想要的,以及是否匹配完了。


你好, 后面还有一个条件“1 shared interest”。 你再试试, 我把自己绕晕了

最起码,后面还有一个
  1. <a title="Ozlem Akgoz’s profile" href="/profile/9575513"><div class="MuiAvatar-root MuiAvatar-circular css-11ml7ev"><img alt="Ozlem Akgoz" src="https://inassets1-outlookgmbh.netdna-ssl.com/static/frontend/images/placeholder/profile/f-250x250.png" class="MuiAvatar-img css-1hy9t21"></div></a><div class="MuiBox-root css-d0uhtl"><h4 class="MuiTypography-root MuiTypography-h4 css-vvcu8p"><div class="css-em857x"><a title="Ozlem Akgoz’s profile" href="/profile/9575513">Ozlem Akgoz</a></div></h4><div class="MuiBox-root css-k008qs"><div class="MuiBox-root css-15ro776"><span aria-hidden="true" title="Turkey" class="css-1208mmm"></span></div><div class="MuiBox-root css-15ro776"><span aria-hidden="true" title="China" class="css-17fnpm9"></span></div></div></div></div></div><div class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-true css-1v9ecy9"><p class="MuiTypography-root MuiTypography-body2 line-clamp-1 css-1o49opv">1 shared interest: Languages &amp; Cultures</p></div><div class="MuiGrid-root MuiGrid-item css-1wxaqej" style="display: flex; justify-content: flex-end;"><button class="MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium MuiButton-disableElevation css-13w4nan" tabindex="0" type="button"
复制代码



这个也符合条件。就是选不出来。但如果把上面符合条件的从源html里删除,这个就能被匹配出来了。  太怪了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-9 14:44:35 | 显示全部楼层
本帖最后由 blackantt 于 2022-10-9 14:53 编辑
wp231957 发表于 2022-10-9 14:06
正则你看7楼吧
我的不是正则:


从来没用过 html2 这种解析,我学习下。
还有一个条件, “1 shared interest” ,我估计就是这个在捣鬼
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-9 14:58:01 | 显示全部楼层
本帖最后由 blackantt 于 2022-10-9 15:02 编辑
阿奇_o 发表于 2022-10-9 13:57
去掉后面的“尾巴”,可以匹配到40个,看看是不是都是你想要的,以及是否匹配完了。


这个尾巴不能去,它有2个title, 还有一个  1 shared interest 的条件
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-9 15:05:04 | 显示全部楼层
blackantt 发表于 2022-10-9 14:44
从来没用过 html2 这种解析,我学习下。
还有一个条件, “1 shared interest” ,我估计就是这个在捣 ...

这个和前面的 也没啥关系啊   这个1 shared interest  有28个   前面的那些有40个   关联不上啊

所以说  还是要看网站源码   然后看你的需求到底是什么

我怎么感觉你的需求很不明白呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-9 15:41:49 | 显示全部楼层
wp231957 发表于 2022-10-9 15:05
这个和前面的 也没啥关系啊   这个1 shared interest  有28个   前面的那些有40个   关联不上啊

所以 ...

除了 1 shared interest  这个条件,还有个  title = China  这个条件。
就是说有 3个,4个,5个 条件的记录,要把5个的这种记录挑出来,但只需要显示这种记录的前3个字段内容
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-9 16:20:11 | 显示全部楼层
blackantt 发表于 2022-10-9 14:44
从来没用过 html2 这种解析,我学习下。
还有一个条件, “1 shared interest” ,我估计就是这个在捣 ...

我只做了最后一个条件,倒数第二个条件 没写,太麻烦了

  1. import requests
  2. from lxml import etree

  3. parse = etree.HTMLParser(encoding="UTF-8")
  4. html = etree.parse('out8.html',parser=parse)   #读取文件方式解析
  5. html2=html.xpath("//div[@role='listitem']")
  6. lst=[]
  7. for x in range(len(html2)):
  8.     h2=html2[x].xpath(".//p['MuiTypography-root MuiTypography-body2 line-clamp-1 css-1o49opv']")
  9.     if h2:
  10.         html3=html2[x].xpath("//div[@class='MuiBox-root css-d0uhtl']")
  11.         data={}
  12.         data["profile"]=html3[x].xpath('./h4/div/a/@href')[0]
  13.         data["text"]=html3[x].xpath('./h4/div/a/text()')[0]
  14.         data["gj"]=html3[x].xpath('./div/div/span/@title')[0]
  15.         lst.append(data)
  16.     else:
  17.         pass   
  18. for x in lst:
  19.     print(x)   
  20. print("共有%d条记录被检索到"%len(lst))   

  21. '''
  22. PS D:\wpp> & D:/Python37/python.exe d:/wpp/test4.py
  23. {'profile': '/profile/9663344', 'text': 'Rui Yang', 'gj': 'China'}
  24. {'profile': '/profile/9675226', 'text': 'zhang denise', 'gj': 'China'}
  25. {'profile': '/profile/9648420', 'text': 'Wang Zixin', 'gj': 'China'}
  26. {'profile': '/profile/9559287', 'text': 'Ye Tao', 'gj': 'China'}
  27. {'profile': '/profile/9643008', 'text': 'Vanessa Fann', 'gj': 'China'}
  28. {'profile': '/profile/9509916', 'text': 'Tafadzwa Sylvester Mashayamombe', 'gj': 'Zimbabwe'}
  29. {'profile': '/profile/9653506', 'text': 'Hanna Aschhoff', 'gj': 'Germany'}
  30. {'profile': '/profile/9665784', 'text': 'Hou Ming', 'gj': 'China'}
  31. {'profile': '/profile/9635533', 'text': 'Eva Wang', 'gj': 'China'}
  32. {'profile': '/profile/9534536', 'text': 'Cedar Chen', 'gj': 'China'}
  33. {'profile': '/profile/9644829', 'text': 'Johnny Lei', 'gj': 'China'}
  34. {'profile': '/profile/9598966', 'text': 'Shakira Han', 'gj': 'China'}
  35. {'profile': '/profile/9533765', 'text': 'Ina Che', 'gj': 'China'}
  36. {'profile': '/profile/9627110', 'text': 'Song Xue', 'gj': 'United States of America'}
  37. {'profile': '/profile/9509995', 'text': 'Maria Gu', 'gj': 'China'}
  38. {'profile': '/profile/9633471', 'text': '婷婷 王', 'gj': 'China'}
  39. {'profile': '/profile/9575439', 'text': 'Jessie Feng', 'gj': 'China'}
  40. {'profile': '/profile/9642985', 'text': 'Kiyono Sakashita', 'gj': 'China'}
  41. {'profile': '/profile/9666052', 'text': 'Mario Ma', 'gj': 'China'}
  42. {'profile': '/profile/9609828', 'text': 'Lea Chang', 'gj': 'China'}
  43. {'profile': '/profile/9665784', 'text': 'Hou Ming', 'gj': 'China'}
  44. {'profile': '/profile/9675226', 'text': 'zhang denise', 'gj': 'China'}
  45. {'profile': '/profile/9524002', 'text': 'Wang phoebe', 'gj': 'China'}
  46. {'profile': '/profile/9543657', 'text': 'Avatar Vanessa Chen', 'gj': 'China'}
  47. {'profile': '/profile/9593679', 'text': 'Gowthm R', 'gj': 'India'}
  48. {'profile': '/profile/9598420', 'text': 'steve wu', 'gj': 'China'}
  49. {'profile': '/profile/9574782', 'text': 'Alex L', 'gj': 'Australia'}
  50. {'profile': '/profile/9575513', 'text': 'Ozlem Akgoz', 'gj': 'Turkey'}
  51. 共有28条记录被检索到
  52. PS D:\wpp>
  53. '''
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-9 16:22:11 | 显示全部楼层
这么复杂,你用正则就不用想了,可以洗洗睡了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-9 16:23:35 | 显示全部楼层
wp231957 发表于 2022-10-9 16:22
这么复杂,你用正则就不用想了,可以洗洗睡了

好,我来学习 html
我觉得就是  .*?  的bug问题。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-9 16:26:07 | 显示全部楼层    本楼为最佳答案   
blackantt 发表于 2022-10-9 16:23
好,我来学习 html
我觉得就是  .*?  的bug问题。

不是html 是lxml  PIP INSTALL LXML
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-9 16:26:34 | 显示全部楼层
wp231957 发表于 2022-10-9 16:26
不是html 是lxml  PIP INSTALL LXML

谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-10 06:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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