无法仅获得 1 次链接
Unable to get only 1 occurrence of links
我在 HTML 的锚标记内有 3 个唯一链接。所有链接出现 2 次。我想做的是使用 python 正则表达式匹配仅获取一次 3 个链接但无法这样做,仅此而已。
这是我的 HTML:
<html>
<body>
<ul class="asidemenu_h1">
<li class="top">
<h3>Mobiles</h3>
</li>
<li>
<a href="http://www.mega.pk/mobiles-apple/" title="Apple Mobiles Price">Apple</a>
</li>
<li>
<a href="http://www.mega.pk/mobiles-asus/" title="Asus Mobiles Price">Asus</a>
</li>
<li>
<a href="http://www.mega.pk/mobiles-black_berry/" title="Black Berry Mobiles Price">Black Berry</a>
</li>
</ul>
<ul class="start2" id="start2ul63" style="visibility: hidden; opacity: 0;">
<li>
<h3>Mobiles</h3>
<ul class="start3 bolder-star">
<li>
<a href="http://www.mega.pk/mobiles-apple/">Apple</a>
</li>
<li>
<a href="http://www.mega.pk/mobiles-asus/">Asus</a>
</li>
<li>
<a href="http://www.mega.pk/mobiles-black_berry/">Black Berry</a>
</li>
</ul>
</li>
</ul>
</body>
</html>
这是我的方法 (1) 使用 for 循环与正则表达式匹配:
for link in soup.find_all("a", href=re.compile(r'(http:\/\/www\.mega\.pk\/mobiles-[A-z]+\/)(?=.*)', re.DOTALL)):
link.get('href')
这个returns什么都没有。
这是我的方法 (2) 使用 for 循环与正则表达式匹配:
for link in soup.find_all("a", href=re.compile(r'(http:\/\/www\.mega\.pk\/mobiles-\w+\/)(?!.*)', re.UNICODE | re.DOTALL)):
link.get('href')
此 returns 重复链接。
获取所有在 href
内具有 mobiles
且带有 CSS selector:
的链接
soup.select("ul.asidemenu_h1 a[href*=mobiles]")
请注意,我限制它在 ul
中搜索具有 asidemenu_h1
class 的链接 - 仅此一项就可以帮助您避免重复。 *=
这里表示 "contains".
如果您坚持使用正则表达式来检查 href
值:
menu = soup.find("ul", class_="asidemenu_h1")
links = menu.find_all("a", href=re.compile(r"mega\.pk\/mobiles-[a-zA-Z0-9_-]+\/$"))
for link in links:
print(link.get_text())
我在 HTML 的锚标记内有 3 个唯一链接。所有链接出现 2 次。我想做的是使用 python 正则表达式匹配仅获取一次 3 个链接但无法这样做,仅此而已。
这是我的 HTML:
<html>
<body>
<ul class="asidemenu_h1">
<li class="top">
<h3>Mobiles</h3>
</li>
<li>
<a href="http://www.mega.pk/mobiles-apple/" title="Apple Mobiles Price">Apple</a>
</li>
<li>
<a href="http://www.mega.pk/mobiles-asus/" title="Asus Mobiles Price">Asus</a>
</li>
<li>
<a href="http://www.mega.pk/mobiles-black_berry/" title="Black Berry Mobiles Price">Black Berry</a>
</li>
</ul>
<ul class="start2" id="start2ul63" style="visibility: hidden; opacity: 0;">
<li>
<h3>Mobiles</h3>
<ul class="start3 bolder-star">
<li>
<a href="http://www.mega.pk/mobiles-apple/">Apple</a>
</li>
<li>
<a href="http://www.mega.pk/mobiles-asus/">Asus</a>
</li>
<li>
<a href="http://www.mega.pk/mobiles-black_berry/">Black Berry</a>
</li>
</ul>
</li>
</ul>
</body>
</html>
这是我的方法 (1) 使用 for 循环与正则表达式匹配:
for link in soup.find_all("a", href=re.compile(r'(http:\/\/www\.mega\.pk\/mobiles-[A-z]+\/)(?=.*)', re.DOTALL)):
link.get('href')
这个returns什么都没有。
这是我的方法 (2) 使用 for 循环与正则表达式匹配:
for link in soup.find_all("a", href=re.compile(r'(http:\/\/www\.mega\.pk\/mobiles-\w+\/)(?!.*)', re.UNICODE | re.DOTALL)):
link.get('href')
此 returns 重复链接。
获取所有在 href
内具有 mobiles
且带有 CSS selector:
soup.select("ul.asidemenu_h1 a[href*=mobiles]")
请注意,我限制它在 ul
中搜索具有 asidemenu_h1
class 的链接 - 仅此一项就可以帮助您避免重复。 *=
这里表示 "contains".
如果您坚持使用正则表达式来检查 href
值:
menu = soup.find("ul", class_="asidemenu_h1")
links = menu.find_all("a", href=re.compile(r"mega\.pk\/mobiles-[a-zA-Z0-9_-]+\/$"))
for link in links:
print(link.get_text())