如何用python查找网站的反向链接

2024-10-01 09:38:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有点困在这种情况下,我想找到网站的反向链接,我找不到怎么做,这是我的正则表达式:

readh = BeautifulSoup(urllib.urlopen("http://www.google.com/").read()).findAll("a",href=re.compile("^http"))

我想做的,找到反向链接,就是,找到以http开头的链接,而不是包含google的链接,我不知道如何管理它?在


Tags: recomhttpread网站链接wwwgoogle
2条回答

下面是一个匹配http页面的regexp,但如果包括google,则不匹配:

re.compile("(?!.*google)^http://(www.)?.*")
from BeautifulSoup import BeautifulSoup
import re

html = """
<div>hello</div>
<a href="/index.html">Not this one</a>"
<a href="http://google.com">Link 1</a>
<a href="http:/amazon.com">Link 2</a>
"""

def processor(tag):
    href = tag.get('href')
    if not href: return False
    return True if (href.find("google") == -1) else False

soup = BeautifulSoup(html)
back_links = soup.findAll(processor, href=re.compile(r"^http"))
print back_links

 output: 
[<a href="http:/amazon.com">Link 2</a>]

然而,只需获取以http开头的所有链接,然后在这些链接中搜索href中没有“google”的链接可能更有效:

^{pr2}$

相关问题 更多 >