鍍金池/ 問答/Python/ pyspider 在index_page中爬取鏈接地址時,如果頁面定義了<

pyspider 在index_page中爬取鏈接地址時,如果頁面定義了<base href="">,則詳細(xì)頁地址獲取錯誤。

1.問題
pyspider 在index_page中爬取鏈接地址時,如果頁面定義了<base href="">,則詳細(xì)頁地址獲取錯誤。
2.代碼

    @every(minutes=5 * 60)
        def on_start(self):
            self.crawl('http://jhsjk.people.cn/result/?keywords=&button=%E6%90%9C%E7%B4%A2', callback=self.index_page)
    
    def index_page(self, response):
            for each in response.doc('div.fr ul.list_14 li a').items():
                print(each.attr.href)
                if each.attr.href.startswith("http://") or each.attr.href.startswith("https://"):
                    self.crawl(each.attr.href, callback=self.detail_page)
                else:
                    self.crawl(urljoin(baseurl, each.attr.href), callback=self.detail_page)

3,程序運行結(jié)果
代碼獲取到的each.attr.href是由http://jhsjk.people.cn/result/。。。 和 鏈接指向的地址如:"article/29613458"合成的,url為http://jhsjk.people.cn/result/article/29613458

但是,實際上頁面自己定義了<base href="http://jhsjk.people.cn/" />,因此,url地址應(yīng)該是http://jhsjk.people.cn/article/29613458。

  1. 希望能夠改正上面的問題
回答
編輯回答
眼雜

沒辦法,程序中強(qiáng)制replace解決。

2018年5月25日 08:35