資源簡介
# 一個使用selemium爬取小說的代碼
理論上使用selemium可以爬取任何站點的資源.可以說是萬能的網(wǎng)絡爬蟲了.這里是演示代碼,業(yè)務邏輯請自己實現(xiàn).
也可以用來做Web程序的自動化測試.

代碼片段和文件信息
#??-*-?coding:?utf-8?-*-
import?os
import?sys
__project_dir__?=?os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if?__project_dir__?not?in?sys.path:
????sys.path.append(__project_dir__)
from?selenium?import?webdriver
from?selenium.webdriver.support.ui?import?WebDriverWait
from?selenium.webdriver.support?import?expected_conditions?as?ec
from?selenium.webdriver.support.select?import?By
from?selenium.webdriver.support.ui?import?WebDriverWait
from?selenium.common.exceptions?import?TimeoutException
from?selenium.webdriver?import?FirefoxProfile
from?selenium.webdriver?import?FirefoxOptions
from?selenium.webdriver?import?Firefox
from?selenium.webdriver.firefox.webdriver?import?FirefoxWebElement
from?selenium.webdriver.chrome.webdriver?import?WebDriver
from?selenium.webdriver?import?ChromeOptions
from?mail_module?import?send_mail
from?log_module?import?get_logger
from?selenium.webdriver?import?Chrome
import?time
import?datetime
“““
https://chromedriver.storage.googleapis.com/index.html?path=2.35/
你也可以自行搜索chromedriver的下載地址解壓是個可執(zhí)行文件放到chrome的目錄即可.
一般ubuntu下面chrome的目錄是/opt/google/chrome/
“““
logger?=?get_logger()
#?chrome_driver?=?“/opt/google/chrome/chromedriver“??#?chromedriver的路徑
chrome_driver?=?os.path.join(__project_dir__?“resource/chromedriver“)??#?chromedriver的路徑
firefox_driver?=?os.path.join(__project_dir__?“resource/geckodriver“)??#?firfoxdriver的路徑
os.environ[“ChromeDriver“]?=?chrome_driver??#?必須配置否則會在execute_script的時候報錯.
#?browser?=?webdriver.Chrome(chrome_driver)
#?wait?=?WebDriverWait(browser?10)
def?get_browser(headless:?bool?=?True?browser_class:?int?=?1)?->?Firefox:
????“““
????獲取一個瀏覽器
????:param?headless:
????:param?browser_class:?瀏覽器種類0是谷歌?1?是火狐?服務器端不能使用谷歌
????:return:
????“““
????“““
????firefox的headless瀏覽器
????因為headless的瀏覽器的語言跟隨操作系統(tǒng)為了保證爬回來的數(shù)據(jù)是正確的語言
????這里必須設置瀏覽器的初始化參數(shù)
????注意,使用headless必須先安裝對應瀏覽器正常的版本然后再安裝headless版本
????比如火狐的headless
????下載火狐的geckodriver驅動。(當前文件夾下已經(jīng)有一個了)地址是:
????https://github.com/mozilla/geckodriver/releases
????下載后解壓是一個geckodriver?文件。拷貝到/usr/local/bin目錄下,然后加上可執(zhí)行的權限
????sudo?chmod?+x?/usr/local/bin/geckodriver
????chrome的headless瀏覽器
????https://chromedriver.storage.googleapis.com/index.html?path=2.35/
????你也可以自行搜索chromedriver的下載地址解壓是個可執(zhí)行文件放到chrome的目錄即可.
????一般ubuntu下面chrome的目錄是/opt/google/chrome/
????據(jù)說使用root權限運行的話chrome的headless瀏覽器會報異常.而firefox的headless瀏覽器不會!
????“““
????if?browser_class?==?1:
????????profile?=?FirefoxProfile()
????????profile.set_preference(“intl.accept_languages“?“zh-cn“)
????????options?=?FirefoxOptions()
????????options.add_argument(“--headless“)
????????if?headless:
????????????try:
????????????????browser?=?Firefox(firefox_profile=profile?executable_path=firefox_driver?firefox_options=options)
????????????except?Exception?as?e:
????????????????title?=?“{}?Firefox?headless瀏覽器打開失敗“.format(datetime.datetime.now())
????????????????content?=?“錯誤原因是:{}“.format(e)
????????????????send_mail(title=title?content=content)
????????????????logger.exception(e)
???????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-10-30?01:45??selenium爬蟲演示代碼\
?????文件?????????237??2018-10-30?01:45??selenium爬蟲演示代碼\readme.md
?????目錄???????????0??2018-10-23?07:25??selenium爬蟲演示代碼\resource\
?????文件?????8103592??2018-05-29?15:44??selenium爬蟲演示代碼\resource\chromedriver
?????文件????12212417??2018-10-04?13:18??selenium爬蟲演示代碼\resource\geckodriver
?????文件???????10911??2018-10-30?01:42??selenium爬蟲演示代碼\browser_module.py
評論
共有 條評論