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

Python,正则表达式:在匹配字符串后提取字符串,python,regex,web-scraping,beautifulsoup,Python,Regex,Web Scraping,Beautifulsoup,我想使用正则表达式匹配模式并提取模式的一部分 我已经收集了HTML数据,一个示例性片段如下所示: </script> </li> <li itemprop="itemListElement" itemscope="" itemtype="http://schema.org/ListItem"> <span class="hide" itemprop="position">1</span> <div class="result-he

我想使用正则表达式匹配模式并提取模式的一部分

我已经收集了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当前存储为BeautifulSoup对象,即它已通过:

html_soup= BeautifulSoup(response.text, 'html.parser')
我想在整个对象中搜索
/projects/
的所有实例,并提取后续斜杠之间的字符串。例如:

from "/projects/quickfixj/" I would like to store "quickfixj".
我最初的想法是使用
re.findall()
并尝试匹配
(/projects/)*
,但这不起作用


非常感谢您的帮助。

您可以提取所有链接,然后应用正则表达式:

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)]
然后,您可以应用您的正则表达式:

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

像这样的正则表达式应该可以做到这一点,
(?您已经完成了一半

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')))

感谢您的回答,我在使用它时遇到了一个问题,主要是因为我没有使用完全具有代表性的示例数据。我已经更新了问题来说明这一点。您将在新数据中注意到,一些以
a
开头的实例具有
href
但不包含
/projects/
的属性,因此在尝试使用
set.add()时
由于要添加的对象为空,将引发一个错误。我是python新手,因此如果您能提供帮助,我将努力解决此问题。谢谢!好的,在添加到集合之前,只需检查对象的
len
cleaned = [re.findall(r'(?<=projects\/)(.*?)\/', i)[0] for i in links]
['quickfixj', 'quickfixj', 'desmoj']
import re
regex = "(?<=\/projects\/).+?(?=\/)"
string = "<a href="/projects/quickfixj/" itemprop="url" title="Find out more...."
matches = re.findall(regex, string)
print(matches)
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')))
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'}