我正在使用 python 脚本从网站 ( http://www.opensiteexplorer.org/ ) 获取特定文本。例如尝试此搜索:http://www.opensiteexplorer.org/links?site=www.google.com
我想获取“页面权限”和“根域”并将其过滤掉。我正在使用 lxml。
我正在使用此代码:
response = br.open( 'http://www.opensiteexplorer.org/links?site=' + blog)
tree = html.fromstring(response.read())
authority = int (tree.xpath('//span[@class="metrics-authority"]/text()')[1].strip())
if authority>1:
print blog
print 'This blog is ready to be registered'
print authority
f.write(blog +' '+ str(authority) +'\n')
在这里,我要过滤大于 1 的 PA,并且我还想过滤大于 5 的链接根域。我该怎么做?
最佳答案
您可以使用metrics-authority
类获取所有2个跨度,第一个是域权限
,第二个是页面权限
。此外,您可以使用 id="metrics-page-link-metrics"
从 div
获取根域
:
import urllib2
from lxml import html
tree = html.parse(urllib2.urlopen('http://www.opensiteexplorer.org/links?site=www.google.com'))
spans = tree.xpath('//span[@class="metrics-authority"]')
data = [item.text.strip() for item in spans]
print "Domain Authority: {0}, Page Authority: {1}".format(*data)
div = tree.xpath('//div[@id="metrics-page-link-metrics"]//div[@class="has-tooltip"]')[1]
print "Root Domains: {0}".format(div.text.strip())
打印:
Domain Authority: 100, Page Authority: 97
Root Domains: 680
希望有帮助。
关于python - 如何使用 Python 从网站获取某些文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22880882/