推荐学习书目
› 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
css3
V2EX  ›  Python

哪位大神能帮忙写一个正则表达式啊?

  •  
  •   css3 · Sep 1, 2018 · 4705 views
    This topic created in 2988 days ago, the information mentioned may be changed or developed.

    如下 ,返回一个 html 页面(类型不是 html 是 str,不能 bs4 解析),想提取里边的 url=https://www.baidu.com/s/1dD8Hkn3 那一串链接,不知道怎么写正则,各种尝试,均不行,取不出来 ,求 v 友帮忙写一个,万谢

    <!DOCTYPE html>
                        <html>
                            <head>    
                            <meta charset="utf-8">    
                            <title>正在跳转...</title>    
                            <meta name="referrer" content="no-referrer" />
                            <meta HTTP-EQUIV="refresh" content="0; url=https://www.baidu.com/s/1dD8Hkn3">
                            </head>
                            <body>
                          </body>
                        </html>
    
    
    30 replies  •  2018-09-03 17:51:17 +08:00
    adminii
    adminii
    Sep 1, 2018
    1
    <meta.+url=(.+)\">$
    直白写入这个不知道是不是符合要求。
    提取$1 就是地址了
    delectate
    delectate
    Sep 1, 2018
    2
    Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import re
    >>> a='''<!DOCTYPE html>
    ... <html>
    ... <head>
    ... <meta charset="utf-8">
    ... <title>正在跳转...</title>
    ... <meta name="referrer" content="no-referrer" />
    ... <meta HTTP-EQUIV="refresh" content="0; url=https://www.baidu.com/s/1dD8Hkn3">
    ... </head>
    ... <body>
    ... </body>
    ... </html>
    ... '''
    >>> re.findall(r'url=(.+)"',a)
    ['https://www.baidu.com/s/1dD8Hkn3']
    >>>
    css3
    css3
    OP
    Sep 1, 2018
    3
    @adminii
    @delectate 非常感谢二位,多谢!
    NickCarter
    NickCarter
    Sep 1, 2018 via iPhone
    4
    locoz
    locoz
    Sep 1, 2018 via Android
    5
    “类型不是 html 是 str ”?这话有点问题
    zaaazaa
    zaaazaa
    Sep 1, 2018
    6
    用 beautifulsoup 先取出片段再用正则会不会简单很多
    zaaazaa
    zaaazaa
    Sep 1, 2018
    7
    好吧居然不能用 bs4
    pppguest3962
    pppguest3962
    Sep 1, 2018
    8
    字符串里面如果还有标签区段,str 可以继续弄进 bs4,再做递进一步的处理的。。。



    ```
    htmlStr = """<!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>正在跳转...</title>
    <meta name="referrer" content="no-referrer" />
    <meta HTTP-EQUIV="refresh" content="0; url=https://www.baidu.com/s/1dD8Hkn3">
    </head>
    <body>
    </body>
    </html>
    """


    htmlStrBs4 = BeautifulSoup(htmlStr.__str__(), 'lxml')
    ```
    zhzer
    zhzer
    Sep 1, 2018
    9
    pattern = r"<meta.+? content=(['"]).+?url=(.+) ?.*\1>"
    组 2 就是链接
    PulpFunction
    PulpFunction
    Sep 1, 2018
    10
    想知道楼主怎么各种尝试的,上切片美滋滋啊
    des
    des
    Sep 1, 2018 via Android
    11
    最近什么回事,看见好几个求正则的了。
    都是做爬虫,你们就不能老老实实用 beautifulsoup 吗??
    angles6645
    angles6645
    Sep 1, 2018 via Android
    12
    你把 str 丢到 bs4 里不是一样吗......
    duan602728596
    duan602728596
    Sep 1, 2018 via iPhone
    13
    python 不是有 pyquery 么
    mmnsghgn
    mmnsghgn
    Sep 1, 2018
    14
    from lxml import etree

    html_string = '''
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>正在跳转...</title>
    <meta name="referrer" content="no-referrer" />
    <meta HTTP-EQUIV="refresh" content="0; url=https://www.baidu.com/s/1dD8Hkn3">
    </head>
    <body>
    </body>
    </html>
    '''

    html = etree.HTML(html_string)

    url = html.xpath('//meta[3]/@content')[0].split('url=')[-1]

    print(url)


    觉得 lxml 比 bs4 舒服的多
    lu5je0
    lu5je0
    Sep 1, 2018
    15
    url=(.*)\"
    xml123
    xml123
    Sep 1, 2018
    16
    @des 可能是因为正则比较简单(门槛低)吧,我第一次写爬虫也是全程正则硬刚……
    des
    des
    Sep 2, 2018 via Android
    17
    @xml123 门槛低倒是,简单就不一定了
    ShareDuck
    ShareDuck
    Sep 2, 2018 via Android❤️1
    18
    每次写正则都要看“ 30 分钟入门”的路过,膜拜各路大神。
    css3
    css3
    OP
    Sep 2, 2018
    19
    @PulpFunction 切片长度一致才行吧,url 这每个长度都不一样啊
    css3
    css3
    OP
    Sep 2, 2018
    20
    @ShareDuck 我也看,但写不出来能用的正则,
    PulpFunction
    PulpFunction
    Sep 2, 2018
    21
    @css3 url 前的字符数不变,url 后面的标签也不会变
    这里的切片和你的 url 长度没关系
    哈哈 我是逗你玩的
    css3
    css3
    OP
    Sep 2, 2018
    22
    @PulpFunction 可以举例说明吗?😀
    richieboy
    richieboy
    Sep 3, 2018
    23
    "(?<=content=\"0; url=)[^\"]*(?=\">)"
    PulpFunction
    PulpFunction
    Sep 3, 2018
    24
    @css3 你自己数吧胸跌
    wersonliu9527
    wersonliu9527
    Sep 3, 2018
    25
    其实用 xpath 我觉得是最简单的,配合谷歌浏览器的插件 xpath helper
    xpresslink
    xpresslink
    Sep 3, 2018
    26
    楼主基本概念都不清楚么?
    只取一个 URL 确实只要用正则就可以了,用 bs4, lxml 之类方案有些重了。
    但是返回一个 html 页面(类型不是 html 是 str,不能 bs4 解析)这是什么屁话。
    css3
    css3
    OP
    Sep 3, 2018
    27
    @xpresslink 不要方,我调用一个接口,人家返回的就是一个 html 页面,类型是 str,有什么好惊讶的呢
    icris
    icris
    Sep 3, 2018
    28
    我来复制一个示例吧

    soup = BeautifulSoup('<b class="boldest">Extremely bold</b>')
    tag = soup.b
    type(tag)
    # <class 'bs4.element.Tag'>

    第一行单引号里面是不是字符串?能不能用 bs4 解析?
    xpresslink
    xpresslink
    Sep 3, 2018
    29
    @css3 我只能呵呵了
    thautwarm
    thautwarm
    Sep 3, 2018
    30
    话说我笔记记得有:

    re_exp = re.compile(r'( https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})')
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2287 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 12:17 · PVG 20:17 · LAX 04:17 · JFK 07:17
    ♥ Do have faith in what you're doing.