鍍金池/ 問答/數據分析&挖掘  Python/ 企查查無頭瀏覽器無法搜索

企查查無頭瀏覽器無法搜索

企查查無法用selenium 無頭瀏覽器搜索

https://www.qichacha.com/

回答
編輯回答
心癌

可能有反爬蟲手段,selenium還是有些特征的,比如全局對象中會有一些特殊屬性。

2018年4月22日 23:39
編輯回答
墻頭草

看你為了爬企查查已經問了不少問題了, 這里我提醒一下:
如果使用 ChromeDriver 的無頭模式, 那么訪問網站的時候不能有通過document.write()插入的 js腳本被執(zhí)行. 參考stackoverflow 上的一個問題:
例子:

>>> from selenium import webdriver
>>> option = webdriver.ChromeOptions()
>>> option.add_argument('--headless')
>>> driver = webdriver.Chrome(chrome_options=option)
[0608/163830.206:ERROR:gpu_process_transport_factory.cc(1007)] Lost UI shared context.

DevTools listening on ws://127.0.0.1:60357/devtools/browser/36a1f861-d1ab-4cef-a5a9-3072bbada0fc
>>> driver.get('https://www.baidu.com')
[0608/163849.677:INFO:CONSOLE(715)] "A parser-blocking, cross site (i.e. different eTLD+1) script, https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/global/js/all_async_search_8d20902.js, is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message. See https://www.chromestatus.com/feature/5718547946799104 for more details.", source: https://www.baidu.com/ (715)

這里 https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/global/js/all_async_search_8d20902.js 是通過 document.write()寫入 html 文本中然后再加載的, 不會被執(zhí)行, 因此報錯.

但是 Firefox 沒有這個問題, 所以我推薦你使用 Firefox 的無頭模式, 或者 phantomjs 這個無頭瀏覽器.
Firefox例子:

from selenium import webdriver
option = webdriver.FirefoxOptions()
option.add_argument('--headless')
driver = webdriver.Firefox(firefox_options=option)
driver.get('https://www.qichacha.com')
# 業(yè)務代碼...

當然, 使用 Firefox 之前需要安裝 Firefox.

2018年6月9日 15:49