如何使用python在网站中查找反向链接[关闭]
Posted
技术标签:
【中文标题】如何使用python在网站中查找反向链接[关闭]【英文标题】:How to find backlinks in a website with python [closed] 【发布时间】:2013-08-16 13:22:08 【问题描述】:我有点被这种情况困住了,我想找到网站的反向链接,我找不到怎么做,这是我的正则表达式:
readh = BeautifulSoup(urllib.urlopen("http://www.google.com/").read()).findAll("a",href=re.compile("^http"))
我想要做的是找到反向链接,就是找到以 http 开头的链接,而不是包含 google 的链接,我不知道如何管理这个?
【问题讨论】:
反向链接与指向页面 x 的链接一样吗?尝试使用 (SEO)moz API? 我想自己写脚本 如果您能够正确获取所有链接,您可以使用此正则表达式(?!.*google)http.*
检查已接受的链接。
@Sniffer 非常感谢 :),这很好,我还有很多东西要学 :)
【参考方案1】:
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”的链接可能更有效:
http_links = soup.findAll('a', href=re.compile(r"^http"))
results = [a for a in http_links if a['href'].find('google') == -1]
print results
--output:--
[<a href="http:/amazon.com">Link 2</a>]
【讨论】:
谢谢你:)) 这个帮助了我:)【参考方案2】:这是一个匹配 http 页面但如果包含 google 则不匹配的正则表达式:
re.compile("(?!.*google)^http://(www.)?.*")
【讨论】:
以上是关于如何使用python在网站中查找反向链接[关闭]的主要内容,如果未能解决你的问题,请参考以下文章