简体   繁体   English

Python,正则表达式:匹配字符串后提取字符串

[英]Python, Regex: Extract string after matching string

I want to use regular expressions to match a pattern and extract a section of the pattern. 我想使用正则表达式匹配模式并提取模式的一部分。

I have scraped HTML data, an illustrative snippet looks like: 我已经抓取了HTML数据,一个说明性代码段如下所示:

</script>
</li>
<li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
<span class="hide" itemprop="position">1</span>
<div class="result-heading">
<a class="project-icon show-outline" href="/projects/quickfixj/" title="Find out more about QuickFIX/J - Open Source Java FIX Engine">
<img alt="QuickFIX/J - Open Source Java FIX Engine Icon" src="//a.fsdn.com/allura/p/quickfixj/icon?1533295730"/></a>
<div class="result-heading-texts">
<a href="/projects/quickfixj/" itemprop="url" title="Find out more 
<a href="/projects/desmoj/" itemprop="url" title="Find out more about DESMO-J"><h2>DESMO-J</h2></a>
<div class="description">
<p class="description-inner">DESMO-<em>J</em> is a framework for 
<a href="/projects/desmoj/files/stats/timeline" title="Downloads This Week">29 This Week</a>
</strong>
<strong>

More representative subset highlighting issue with find_all('a') : find_all('a')更具代表性的子集突出显示问题:

<!-- Menu -->
<ul class="header-nav-menulist">
<li class="highlight social row">
<span class="social-label">Connect</span>
<span class="social-icons">
<span></span>
<a class="twitter" href="https://twitter.com/sourceforge" rel="nofollow" target="_blank">
<svg viewbox="0 0 1792 1792" xmlns="http://www.w3.org/2000/svg"><path d="M1684 408q-67 98-162 167 1 14 1 42 0 130-38 259.5t-115.5 248.5-184.5 210.5-258 146-323 54.5q-271 0-496-145 35 4 78 4 225 0 401-138-105-2-188-64.5t-114-159.5q33 5 61 5 43 0 85-11-112-23-185.5-111.5t-73.5-205.5v-4q68 38 146 41-66-44-105-115t-39-154q0-88 44-163 121 149 294.5 238.5t371.5 99.5q-8-38-8-74 0-134 94.5-228.5t228.5-94.5q140 0 236 102 109-21 205-78-37 115-142 178 93-10 186-50z"></path></svg></a>
<a class="facebook" href="https://www.facebook.com/sourceforgenet/" rel="nofollow" target="_blank">

The HTML is currently stored as a BeautifulSoup object, ie it has been passed through: HTML当前存储为BeautifulSoup对象,即已通过以下方式传递:

html_soup= BeautifulSoup(response.text, 'html.parser')

I would like to search this entire object for all instances of /projects/ and extract the string between the subsequent slashes. 我想在整个对象中搜索/projects/所有实例,并提取后续斜杠之间的字符串。 For example: 例如:

from "/projects/quickfixj/" I would like to store "quickfixj".

My initial idea is to use re.findall() and try to match (/projects/./)* but this does not work. 我最初的想法是使用re.findall()并尝试匹配(/projects/./)*但这不起作用。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

You are already half way through 您已经完成了一半

a='''</script>
</li>
<li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
<span class="hide" itemprop="position">1</span>
<div class="result-heading">
<a class="project-icon show-outline" href="/projects/quickfixj/" title="Find out more about QuickFIX/J - Open Source Java FIX Engine">
<img alt="QuickFIX/J - Open Source Java FIX Engine Icon" src="//a.fsdn.com/allura/p/quickfixj/icon?1533295730"/></a>
<div class="result-heading-texts">
<a href="/projects/quickfixj/" itemprop="url" title="Find out more 
<a href="/projects/desmoj/" itemprop="url" title="Find out more about DESMO-J"><h2>DESMO-J</h2></a>
<div class="description">
<p class="description-inner">DESMO-<em>J</em> is a framework for 
<a href="/projects/desmoj/files/stats/timeline" title="Downloads This Week">29 This Week</a>
</strong>
<strong>'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(a,"html.parser")
for i in soup.find_all('a'):
    print(re.findall('/projects/(\w{1,})/',i.get('href')))

In case you need unique projects. 如果您需要独特的项目。 Change last few line to 将最后几行更改为

from bs4 import BeautifulSoup
soup = BeautifulSoup(a,"html.parser")
project_set=set()
for i in soup.find_all('a'):
    project_set.add(*re.findall('/projects/(\w{1,})/',i.get('href')))

print(project_set) #{u'desmoj', u'quickfixj'}

You can extract all of the links and then apply a regex: 您可以提取所有链接,然后应用正则表达式:

from bs4 import BeautifulSoup

html = '''</script>
</li>
<li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem">
<span class="hide" itemprop="position">1</span>
<div class="result-heading">
<a class="project-icon show-outline" href="/projects/quickfixj/" title="Find out more about QuickFIX/J - Open Source Java FIX Engine">
<img alt="QuickFIX/J - Open Source Java FIX Engine Icon" src="//a.fsdn.com/allura/p/quickfixj/icon?1533295730"/></a>
<div class="result-heading-texts">
<a href="/projects/quickfixj/" itemprop="url" title="Find out more 
<a href="/projects/desmoj/" itemprop="url" title="Find out more about DESMO-J"><h2>DESMO-J</h2></a>
<div class="description">
<p class="description-inner">DESMO-<em>J</em> is a framework for 
<a href="/projects/desmoj/files/stats/timeline" title="Downloads This Week">29 This Week</a>
</strong>
<strong>'''

html_soup = BeautifulSoup(html, 'html.parser')

links = [i.get('href') for i in html_soup.find_all('a', href=True)]

Yields: 产量:

['/projects/quickfixj/', '/projects/quickfixj/', '/projects/desmoj/files/stats/timeline']

Then you can apply your regex: 然后,您可以应用正则表达式:

cleaned = [re.findall(r'(?<=projects\/)(.*?)\/', i)[0] for i in links]

Yields: 产量:

['quickfixj', 'quickfixj', 'desmoj']

A Regex like this should do the trick (?<=\\/projects\\/).+?(?=\\/) 这样的正则表达式应该可以解决问题(?<=\\/projects\\/).+?(?=\\/)

And would work like this 并且会像这样工作

import re
regex = "(?<=\/projects\/).+?(?=\/)"
string = "<a href="/projects/quickfixj/" itemprop="url" title="Find out more...."
matches = re.findall(regex, string)
print(matches)

Output: ["quickfixj"] 输出: ["quickfixj"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM