【问题标题】:Python regular expression match start and end string and must contain specific wordPython 正则表达式匹配开始和结束字符串,并且必须包含特定的单词
【发布时间】:2019-11-28 03:43:46
【问题描述】:

我需要一些指导来完善我的正则表达式。我有一个网页的来源,并想从页面中提取href。该表没有任何 ID 或类。我决定使用正则表达式,但是我的表达式似乎比我想要的要匹配。 我尝试了以下方法:

http:\/\/(.*?)(?=.*showuri)(.*?)responseType=xml\">\/lnc\/

我的开始是 http:// 结束是 responseType=xml">/lnc/ 我需要中间位来包含单词 showuri

我正在使用 Python 3

【问题讨论】:

  • 也许你应该首先使用解析器来获取所有href(参见this post)然后过滤包含responseType=xml>/lnc/的结果
  • 不要为此使用 RegEx。

标签: python regex python-3.x regex-group


【解决方案1】:

我使用的方法如下:

doc = html.fromstring(text)
tr_elements = doc.xpath('//a/@href')
df = pd.DataFrame(tr_elements)
df.columns=['URL']

从这里开始,我将删除不包含“showuri”的行

【讨论】:

  • 感谢@ctwheels 提供了类似的处理方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多