0

I want to make a tool to analyse the result of code search engine like Krugle or OpenHub. I've tried java and python to get html page of search result:

import urllib2
def write_url(url, file_name, if_show):    
    if (url is None) or (file_name is None):
        return

    req = urllib2.Request(url)
    resp = urllib2.urlopen(req)

    ret = resp.read()

    fp = open(file_name, "w")
    fp.write(ret)
    fp.close()
    if if_show:
        print ret


if __name__ == "__main__":
    url_ = "http://www.krugle.org/document/search/#query=socket"
    file_n = "D:/tmp/test.txt"
    write_url(url_, file_n, True)
    print "Done"

but I didn't get the content of the result. Part of the page I got is like this:

            <div class="content_result_body">
                <div id="hit_list"></div>
                <div class="paging" style="display: none;"></div>
            </div>

I used chrome to check the result page of a search. It's something like this:

            <div class="content_result_body">
                <div id="hit_list">
                    <div class="hit">...</div>
                    <div class="hit">...</div>
                    <div class="hit">...</div>
                </div>
                <div class="paging">...</div>
            </div>

And in the div.hit, the "..." stands for the content of the result Krugle searched. I'm not sure why there were nothing in the div.hit_list of the returned page I got by my python code. Maybe the content of the result was generated by js. But I don't know how to get it by codes.

4

1 回答 1

0

处理动态加载内容的页面,可以用 Selenium 试试,

from selenium import webdriver

url = "your-url.com"
br = webdriver.Firefox()
br.get(url)

html = br.page_source

当然,这也会打开一个网络浏览器。如果那不方便,我可以告诉你如何使用 xvfb 或 phantomjs

于 2014-11-24T13:41:14.507 回答