V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 4254 views
    This topic created in 2796 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
        1
    adminii  
       Sep 1, 2018
    <meta.+url=(.+)\">$
    直白写入这个不知道是不是符合要求。
    提取$1 就是地址了
    delectate
        2
    delectate  
       Sep 1, 2018
    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
        3
    css3  
    OP
       Sep 1, 2018
    @adminii
    @delectate 非常感谢二位,多谢!
    NickCarter
        4
    NickCarter  
       Sep 1, 2018 via iPhone
    locoz
        5
    locoz  
       Sep 1, 2018 via Android
    “类型不是 html 是 str ”?这话有点问题
    zaaazaa
        6
    zaaazaa  
       Sep 1, 2018
    用 beautifulsoup 先取出片段再用正则会不会简单很多
    zaaazaa
        7
    zaaazaa  
       Sep 1, 2018
    好吧居然不能用 bs4
    pppguest3962
        8
    pppguest3962  
       Sep 1, 2018
    字符串里面如果还有标签区段,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
        9
    zhzer  
       Sep 1, 2018
    pattern = r"<meta.+? content=(['"]).+?url=(.+) ?.*\1>"
    组 2 就是链接
    PulpFunction
        10
    PulpFunction  
       Sep 1, 2018
    想知道楼主怎么各种尝试的,上切片美滋滋啊
    des
        11
    des  
       Sep 1, 2018 via Android
    最近什么回事,看见好几个求正则的了。
    都是做爬虫,你们就不能老老实实用 beautifulsoup 吗??
    zst
        12
    zst  
       Sep 1, 2018 via Android
    你把 str 丢到 bs4 里不是一样吗......
    duan602728596
        13
    duan602728596  
       Sep 1, 2018 via iPhone
    python 不是有 pyquery 么
    mmnsghgn
        14
    mmnsghgn  
       Sep 1, 2018
    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
        15
    lu5je0  
       Sep 1, 2018
    url=(.*)\"
    xml123
        16
    xml123  
       Sep 1, 2018
    @des 可能是因为正则比较简单(门槛低)吧,我第一次写爬虫也是全程正则硬刚……
    des
        17
    des  
       Sep 2, 2018 via Android
    @xml123 门槛低倒是,简单就不一定了
    ShareDuck
        18
    ShareDuck  
       Sep 2, 2018 via Android   ❤️ 1
    每次写正则都要看“ 30 分钟入门”的路过,膜拜各路大神。
    css3
        19
    css3  
    OP
       Sep 2, 2018
    @PulpFunction 切片长度一致才行吧,url 这每个长度都不一样啊
    css3
        20
    css3  
    OP
       Sep 2, 2018
    @ShareDuck 我也看,但写不出来能用的正则,
    PulpFunction
        21
    PulpFunction  
       Sep 2, 2018
    @css3 url 前的字符数不变,url 后面的标签也不会变
    这里的切片和你的 url 长度没关系
    哈哈 我是逗你玩的
    css3
        22
    css3  
    OP
       Sep 2, 2018
    @PulpFunction 可以举例说明吗?😀
    richieboy
        23
    richieboy  
       Sep 3, 2018
    "(?<=content=\"0; url=)[^\"]*(?=\">)"
    PulpFunction
        24
    PulpFunction  
       Sep 3, 2018
    @css3 你自己数吧胸跌
    wersonliu9527
        25
    wersonliu9527  
       Sep 3, 2018
    其实用 xpath 我觉得是最简单的,配合谷歌浏览器的插件 xpath helper
    xpresslink
        26
    xpresslink  
       Sep 3, 2018
    楼主基本概念都不清楚么?
    只取一个 URL 确实只要用正则就可以了,用 bs4, lxml 之类方案有些重了。
    但是返回一个 html 页面(类型不是 html 是 str,不能 bs4 解析)这是什么屁话。
    css3
        27
    css3  
    OP
       Sep 3, 2018
    @xpresslink 不要方,我调用一个接口,人家返回的就是一个 html 页面,类型是 str,有什么好惊讶的呢
    icris
        28
    icris  
       Sep 3, 2018
    我来复制一个示例吧

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

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

    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   ·   Solana   ·   5333 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 71ms · UTC 09:40 · PVG 17:40 · LAX 02:40 · JFK 05:40
    ♥ Do have faith in what you're doing.