1. 程式人生 > >scrapy使用PhantomJS和selenium爬取數據

scrapy使用PhantomJS和selenium爬取數據

html oca col spa def bsp ret tar self

1.phantomjs 安裝

下載:http://phantomjs.org/download.html

解壓:

tar  -jxvf  phantomjs-2.1.1-linux-x86_64.tar.bz2

重命名:

mv /usr/local/phantomjs-2.1.1-linux-x86_64/ /usr/local/phantomjs

軟連接:

ln -s /usr/local/phantomjs/bin/phantomjs /usr/bin/ 

[root@izuf622gt8apcfsz7i1mqdz /]# phantomjs
phantomjs>

2.selenium 安裝

pip 安裝: pip install selenium

使用:

    def process_request(self, request, spider):
        driver = webdriver.PhantomJS()
        # driver = webdriver.Chrome()
        driver.get(request.url)
        body = driver.page_source
        input_first  = driver.find_element_by_id(stockID_)
        input_first.clear()

        input_first.send_keys(
000150) button = driver.find_element_by_id(button) dataClick = button.click() print(dataClick) body = driver.page_source # driver.switch_to.frame(‘i_nr‘) # print("訪問:", driver.page_source) return HtmlResponse(driver.current_url, body=body, encoding=
utf-8)

scrapy使用PhantomJS和selenium爬取數據