Python 爬蟲實例(8)—— 爬取 動態頁面


今天使用python 和selenium爬取動態數據,主要是通過不停的更新頁面,實現數據的爬取,要爬取的數據如下圖

 

 

源代碼:

#-*-coding:utf-8-*-
import time
from selenium import webdriver
import os
import re
#引入chromedriver.exe
chromedriver = "C:/Users/xuchunlin/AppData/Local/Google/Chrome/Application/chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
browser = webdriver.Chrome(chromedriver)

#設置瀏覽器需要打開的url
url = "https://www.jin10.com/"
# 使用for循環不停的刷新頁面,也可以每隔一段時間刷新頁面
for i in range(1,100000):
    browser.get(url)
    result= browser.page_source
    gold_price = ""
    gold_price_change = ""
    try:
        gold_price = re.findall('<div id="XAUUSD_B" class="jin-price_value" style=".*?">(.*?)</div>',result)[0]
        gold_price_change = re.findall('<div id="XAUUSD_P" class="jin-price_value" style=".*?">(.*?)</div>',result)[0]
    except:
        gold_pric = "------"
        gold_price_change = "------"

    print gold_price
    print gold_price_change
    time.sleep(1)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM