免费注册 查看新帖 |

ChinaUnix.net

  平台 论坛 博客 文库 频道自动化运维 虚拟化 储存备份 C/C++ PHP MySQL 嵌入式 Linux系统
最近访问板块 发新帖
楼主: 51itpub

正则表达式问题 [复制链接]

论坛徽章:
0
发表于 2018-04-18 14:27 |显示全部楼层
(.*)?  -->  (.*?)

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-04-12 06:20:00
发表于 2018-04-18 15:23 |显示全部楼层
dahe_1984 发表于 2018-04-18 13:43
BeautifulSoup 这个模块用的不熟悉,估计find_all后可以直接找出.*。


谢谢dahe_1984,代码有问题。

    soup=BeautifulSoup(string,'html.parse')
NameError: name 'string' is not defined

Process finished with exit code 1


直接把要解析的字符窜替换string,还是报错:
    soup=BeautifulSoup(lines,'html.parse')
  File "C:\Users\F\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\__init__.py", line 165, in __init__
    % ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html.parse. Do you need to install a parser library?




论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-04-12 06:20:00
发表于 2018-04-18 15:24 |显示全部楼层
dahe_1984 发表于 2018-04-18 13:43
BeautifulSoup 这个模块用的不熟悉,估计find_all后可以直接找出.*。


谢谢dahe_1984,代码有问题。

    soup=BeautifulSoup(string,'html.parse')
NameError: name 'string' is not defined

Process finished with exit code 1


直接把要解析的字符窜替换string,还是报错:
    soup=BeautifulSoup(lines,'html.parse')

bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html.parse. Do you need to install a parser library?

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-04-12 06:20:00
发表于 2018-04-18 15:27 |显示全部楼层
谢谢dahe_1984,

soup = BeautifulSoup(string,
这里报NameError: name 'string' is not defined

论坛徽章:
0
发表于 2018-04-18 15:29 |显示全部楼层
import re
from bs4 import BeautifulSoup

  1. string = '20180210 10:10:45【队列自动】:10001<Root><Head><AA>QQ808311</AA><CommandCode>10001</CommandCode><Seq>201802101010431</Seq><VerifyCode></VerifyCode><BB>103</BB></Head><NoticeReq><CC>QQ808311_312020180210_802101008493712.s</CC><SeqtNo>802101008493712</SeqtNo><W5>3</W5><TotalNum>2</TotalNum><DD>687800</DD><W1>01</W1><AccType>1</AccType><W2>1</W2><W3><EncryptFlag>1</EncryptFlag><SourceDataMD5></SourceDataMD5><EncryptDD></EncryptDD></W3></NoticeReq></Root>20180210 10:10:46【队列自动】报文:10001<Root><NoticeResp><W4>通知受理成功</W4><ResultState>2</ResultState><ZZ>19000210000000044087</ZZ><ZA></ZA><SeqtNo>802101008493712</SeqtNo><CC>QQ808311_312020180210_802101008493712.s</CC></NoticeResp><Head><VerifyCode></VerifyCode><Seq>201802101010431</Seq><CommandCode>10001</CommandCode><AA>QQ808311</AA></Head></Root>20180210 10:10:46发送通知成功20180210 10:10:46成功标志文件已保存。20180210 10:10:46状态2更新成功。'
复制代码


root_p = re.compile(r'<Root>(.*?)</Root>', re.IGNORECASE)

soup = BeautifulSoup(string, 'html.parser')


for tmp in soup.find_all(name = 'root'):
    searchobj=re.findall(root_p,str(tmp))
    print(searchobj)

论坛徽章:
0
发表于 2018-04-18 15:29 |显示全部楼层
string 就是你的文本啊,怎么会string没有定义?

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-04-12 06:20:00
发表于 2018-04-18 15:35 |显示全部楼层

谢谢black_bean,
这个可行!
粗看,?放在括号外面好像达意,放在括号内部应达意。
正则表达式还得努力学习。。。

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-04-12 06:20:00
发表于 2018-04-18 15:39 |显示全部楼层
dahe_1984 发表于 2018-04-18 15:29
import re
from bs4 import BeautifulSoup



谢谢dahe_1984,

我当前环境可能少什么包了,parser library

    soup=BeautifulSoup(lines,'html.parse')
  File "C:\Users\F\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\__init__.py", line 165, in __init__
    % ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html.parse. Do you need to install a parser library?


论坛徽章:
0
发表于 2018-04-18 16:53 |显示全部楼层
pip install lxml 然后试试

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2016-04-12 06:20:00
发表于 2018-04-18 17:35 |显示全部楼层
dahe_1984 发表于 2018-04-18 16:53
pip install lxml 然后试试

谢谢dahe_1984!
还是少包。。。

>>> root_p = re.compile(r'<Root>(.*?)</Root>', re.IGNORECASE)
>>> soup=BeautifulSoup(lines,'html.parse')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\F\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\__init__.py", line 165, in __init__
    % ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html.parse. Do you need to install a parser library?
>>> import lxml
>>> soup=BeautifulSoup(lines,'html.parse')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\F\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\__init__.py", line 165, in __init__
    % ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html.parse. Do you need to install a parser library?

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

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号 北京市公安局海淀分局网监中心备案编号:11010802020122
广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员  联系我们:wangnan@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP