提取Google搜索结果
问题描述:
我想定期检查Google列出了哪些子域。提取Google搜索结果
要获取子域列表,我在Google搜索框中键入'site:example.com' - 它列出了所有子域名结果(我们域名超过20页)。
什么是最好的方式来提取只有由'网站:example.com'搜索返回的地址的网址?
我正在考虑编写一个小的python脚本,它将执行上述搜索并从搜索结果中对URL进行正则表达式(在所有结果页面上重复)。这是一个好的开始?有没有更好的方法?
干杯。
答
正则表达式是用于解析HTML一个坏主意。它阅读起来很神秘,依赖于格式良好的HTML。
尝试使用Python的BeautifulSoup。以下是一个示例脚本,用于返回网站前10个网页的网址:domain.com Google查询。
import sys # Used to add the BeautifulSoup folder the import path
import urllib2 # Used to read the html document
if __name__ == "__main__":
### Import Beautiful Soup
### Here, I have the BeautifulSoup folder in the level of this Python script
### So I need to tell Python where to look.
sys.path.append("./BeautifulSoup")
from BeautifulSoup import BeautifulSoup
### Create opener with Google-friendly user agent
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
### Open page & generate soup
### the "start" variable will be used to iterate through 10 pages.
for start in range(0,10):
url = "http://www.google.com/search?q=site:*.com&start=" + str(start*10)
page = opener.open(url)
soup = BeautifulSoup(page)
### Parse and find
### Looks like google contains URLs in <cite> tags.
### So for each cite tag on each page (10), print its contents (url)
for cite in soup.findAll('cite'):
print cite.text
输出:
*.com/
*.com/questions
*.com/unanswered
*.com/users
meta.*.com/
blog.*.com/
chat.meta.*.com/
...
当然,你可以每个结果追加到一个列表,以便您可以分析它的子域。我刚刚进入Python并且在几天前进行了修改,但是这会让你开始。
或采用JSON格式,这非常容易在Python中使用(使用`json`模块) – 2010-12-07 00:06:47
AFAIK自定义搜索不允许搜索整个网络,只是选择的域。搜索的重点可能是浏览所有网页:) – 2011-08-17 16:05:09