麻烦帮忙看一下这个「正则表达式」? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
zippera
V2EX    Python

麻烦帮忙看一下这个「正则表达式」?

  •  
  •   zippera
    zippera 2013-07-27 11:16:42 +08:00 7042 次点击
    这是一个创建于 4484 天前的主题,其中的信息可能已经有所发展或是发生改变。
    <div class="content" title="2013-07-25 09:09:58">

    要匹配的内容

    </div>

    re:
    pat = re.compile(r'<div.*?class="content".*?ti.*?>(.*?)</div>')
    pat.findall(ucpage, re.S)
    ...


    没有匹配的结果,正则表达式哪里出问题了?
    19 条回复    1970-01-01 08:00:00 +08:00
    timonwong
        1
    timonwong  
       2013-07-27 11:23:16 +08:00
    re.MULTILINE
    timonwong
        2
    timonwong  
       2013-07-27 11:23:55 +08:00
    手滑,应该是 re.DOTALL
    lyjyiran
        3
    lyjyiran  
       2013-07-27 11:25:58 +08:00
    下面findall没re.S这个参数

    re.compile那行re.I改成re.I | re.S

    对html操作, 建议别用正则,用bs4、lxml、pyquery这些比较好
    suchj
        4
    suchj  
       2013-07-27 12:22:03 +08:00
    你的内容是换行的吧,试试换行模式吧,python2.7.5下测试可行
    http://gist.github.com/miraclesu/6093693
    sujin190
        5
    sujin190  
       2013-07-27 12:34:09 +08:00
    未启用多行模式
    zippera
        6
    zippera  
    OP
       2013-07-27 12:34:10 +08:00
    @timonwong
    @lyjyiran 多谢,我都没注意到放错位置了。

    初学爬虫,先用正则,lxml、pyquery、bs后面会学。

    还有另外一个问题:
    用 f.writelines()保存文本到txt文件中,出现错误:

    TypeError: writelines() argument must be a sequence of strings

    是编码的问题吗,我已经用decode("utf-8")转码了啊
    zippera
        7
    zippera  
    OP
       2013-07-27 12:36:08 +08:00
    @suchj 的确是这样,谢谢,麻烦帮忙再看下6楼那个问题
    pandada8
        8
    pandada8  
       2013-07-27 20:34:07 +08:00
    @zippera writelines() 传入的参数是一个序列 []
    不知道是不是这个问题
    ccdjh
        9
    ccdjh  
       2013-07-27 21:16:06 +08:00
    去一下空格和换行
    内容.replace(' ','')
    内容.replace('\n','')
    内容.replace('\r','')
    zippera
        10
    zippera  
    OP
       2013-07-27 21:33:42 +08:00
    @pandada8
    @ccdjh
    好像就是符号的问题。还是不行,请帮忙看看,贴代码:(BTW,除了replace有更好的办法吗)

    <script src="https://gist.github.com/zippera/6094881.js"></script>
    zippera
        11
    zippera  
    OP
       2013-07-27 21:36:42 +08:00
    timonwong
        12
    timonwong  
       2013-07-27 21:45:12 +08:00
    @zippera
    writelines需要写一个字符串序列(比如字符串列表),但是item是字符串。
    你需要使用write()
    pandada8
        13
    pandada8  
       2013-07-27 21:50:53 +08:00
    writelines(lines):
    Write a list of lines to the stream.

    #所以要用writelines的话应该要传入一个列表或元组

    这里应该可以直接write(item+"\n")

    处理网页最好还是用 BeautiulSoup

    最好把文件打开关闭的操作放到For循环外面
    或者你可以mat中所有的数据处理后在使用 writelines 一次写入
    注意writelines 写入时不会帮你加回车

    if len(mat)似乎可以去掉
    python的for语句也是有 else 语句块的
    当mat为空时,执行else语句块
    clowwindy
        14
    clowwindy  
       2013-07-27 21:53:54 +08:00
    HTML 不是正则语言,不要用正则表达式处理。
    pandada8
        15
    pandada8  
       2013-07-27 21:59:37 +08:00
    写入时在我这里报了一个
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-16: ordinal not in range(128)

    大约你可以看看

    http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html

    //快用Python3啊,Utf-8解脱一切啊
    zippera
        16
    zippera  
    OP
       2013-07-27 22:11:08 +08:00
    @pandada8 编码问题真麻烦。刚开始学习,漏洞百出,多谢你的这么多建议。
    @timonwong 的确是,我没注意
    @clowwindy 我看好多人都用正则,而且据说比其他工具快
    ccdjh
        17
    ccdjh  
       2013-07-27 23:20:29 +08:00
    提取content里面的内容,对吧?直接shell哈,我就不整理了

    import re
    import urllib

    url = "http://www.qiushibaike.com/month/page/"
    r = urllib.urlopen(url).read()
    html = r
    strlist = html.split("""<div class="block untagged mb15 bs2" id=""")
    len(strlist)
    d = strlist[2]
    tmp = d.replace(' ','')
    tmp2 = tmp.replace('\n','')
    tmp3 = tmp2.replace('\r','')
    f = re.findall('''<divclass="content"title=(.*?)</div>''',tmp3)
    binux
        18
    binux  
       2013-07-27 23:30:51 +08:00
    @zippera 快是说如果你的问题能直接用正则表示出来,那么很快。你看你这里明明就不可以
    iveney
        19
    iveney  
       2013-07-28 05:22:53 +08:00
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1296 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 17:13 PVG 01:13 LAX 09:13 JFK 12:13
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86