【问题标题】:regex number in href url in pythonpython中href url中的正则表达式数字
【发布时间】:2019-05-10 00:19:56
【问题描述】:

我正在尝试从这样的 URL 中提取一个数字: https://ghostbin.com/paste/dmjvt 我想提取数字 3 我设法用 beatifulsoup 从 \li 提取到 /li,我似乎无法设法做一个正则表达式来从 \a href 中提取这个单个数字,因为它后面可能有一些数字 什么是 python 中的正则表达式来完成这个? 谢谢

【问题讨论】:

  • 好吧,我没有看到提供的足够信息。请添加足够的信息
  • 那里缺少什么?我的工作块是ghostbin.com/paste/2hdyd,我想在 /page/ 和 /# 之前提取数字,但问题是, /page/ 之前可能有数字。然后如何使用 python 正则表达式提取它?
  • 请将您链接到的数据添加到问题中。链接可能会过期,从而使您的问题对未来的读者毫无意义。
  • 我试过了,但问题真的搞砸了,而且我设置了粘贴不会过期。
  • @j.doe 你可以看到这个regex101.com/r/7lafAJ/1

标签: python regex beautifulsoup


【解决方案1】:

尝试使用这个正则表达式/page/(\d+)/

import re
from bs4 import BeautifulSoup

html = '''<li class="page-item pagination-end">
  <a class="page-link page-text" href="xxx/page/3/#filters">3</a>
</li>'''

soup = BeautifulSoup(html, 'html.parser')
endNav = soup.select_one('.page-item.pagination-end a')
navNumber = re.search(r'/page/(\d+)/', endNav['href']).group(1)
print(navNumber) # 3

【讨论】:

  • 感谢您的帮助,这是关于实施的......!
猜你喜欢
  • 1970-01-01
  • 2010-12-31
  • 2011-07-09
  • 2021-03-30
  • 2020-08-10
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多