按教程用正则表达式提取不到内容怎么办? - 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
xiaozaiziwyt

按教程用正则表达提取不到内容怎么办?

  •  
  •   xiaozaiziwyt Feb 24, 2019 2544 views
    This topic created in 2637 days ago, the information mentioned may be changed or developed.
    爬取猫眼 100 名电影,结果检测正则表达式提取内容的时候返回的结果为空。

    def parse_one_page(html):
    pattern = re.compile(
    '<dd>.*?board-index.*?>(.*?)</i>.*?data-src="(.*?)".*?name.*?a.*?>(.*?)</a>.*?star.*?>(.*?)</p>.*?releasetime.*?>(.*?)</p>.*?integer.*?>(.*?)</i>.*?fraction.*?>(.*?)</i>.*?</dd>',
    re.S)
    items = re.findall(pattern, html)
    print(items)
    这是第一个;

    import re
    def parse_one_page(html):

    pattern = re.compile('<dd>.*?board-index.*?>(\d+)</i>.*?data-src="(.*?)".*?name">'
    + '<a.*?>(.*?)</a>.*?"star">(.*?)</p>.*?releasetime">(.*?)</p>'
    + '.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>', re.S)

    items = re.findall(pattern, html)

    for item in items:
    yield {
    'index': item[0],
    'image': item[1],
    'title': item[2],
    'actor': item[3].strip()[3:],
    'time': item[4].strip()[5:],
    'score': item[5] + item[6]
    }
    def main():
    url = 'http://maoyan.com/board/4'
    html = get_one_page(url)
    for item in parse_one_page(html):
    print(item)
    这是第二种方式。
    发现都提取不出来内容,但是如果用完整的代码则在最后运行的时候会正确显示……
    8 replies    2019-02-25 16:37:12 +08:00
    Kacxxia
        1
    Kacxxia  
       Feb 24, 2019   1
    https://regex101.com
    推荐你用这个测试正则,右上会有语法解析
    xiaozaiziwyt
        2
    xiaozaiziwyt  
    OP
       Feb 24, 2019
    @Kacxxia 谢谢了。不过重新写代码后发现居然能运行了
    fzinfz
        3
    fzinfz  
       Feb 25, 2019   1
    写这么长正则解析 html 的教程是为了找人切磋不是教人的吧,劝楼主另寻教程。。。关键词:bs4
    msg7086
        4
    msg7086  
       Feb 25, 2019
    比较好的办法是先用正则提取出主要数据,再用 XML / HTML 解析器解成结构化数据,再去读取遍历。
    hakono
        5
    hakono  
       Feb 25 2019 via Android
    楼主你看的么破教程啊,教人抽取复杂的网页用正则。。。。
    乖乖去用 beautiful soup 去,一个 css 选择器就抽出来,节省自己点时间和生命吧
    xpresslink
        6
    xpresslink  
       Feb 25, 2019
    建议楼主学习 一下 xPath 语法,写代码效率比正则要高多了,在 scrapy 中直接就可以用。 比较容易入手还是推荐 BS4
    E1n
        7
    E1n  
       Feb 25, 2019 via Android
    正则写的爽
    hjq98765
        8
    hjq98765  
       Feb 25, 2019
    bs4+1
    About     Help     Advertise     Blog     API     FAQ     Solana     888 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 61ms UTC 21:34 PVG 05:34 LAX 14:34 JFK 17:34
    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