可以使用以下正则表达式提取该代码中的域名:

pattern = r"<a class='weburl demo' href='(.*?)'" 

解释:

  • <a class='weburl demo' href=': 匹配 <a class='weburl demo' href=' 字符串。
  • (.*?): 使用非贪婪模式匹配任意字符,直到下一个字符匹配成功。
  • ': 匹配右单引号,结束匹配。

完整代码如下:

import re

html_code = """
<div class='ly'>
    <span></span>
    <span class='cdes'>域名是17年注册的干净域名,一直养到现在,很好用。网站SEO基础好,稳定日收,历史多次达到权6,高峰移动权7。目前百度...
    </span>
    <cite>
        <a class='webinfo' href='https://web.huzhan.com/6576/' target='_blank' title='查看网站详情'><em>&#xe630;</em>查看详情</a>
        <a class='weburl demo' href='http://www.edu28.cn' target='_blank' rel='nofollow' mode='web' tips='0' title='直接浏览网站'><em>&#xe664;</em>浏览网站</a>
    </cite>
</div>
"""

pattern = r"<a class='weburl demo' href='(.*?)'" 
match = re.search(pattern, html_code)

if match:
    domain = match.group(1)
    print(domain)
else:
    print("未找到域名")

输出:

http://www.edu28.cn
Python 正则表达式提取代码中的域名

原文地址: https://www.cveoy.top/t/topic/ncYU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录