今天使用python 和selenium爬取動態數據,主要是經過不停的更新頁面,實現數據的爬取,要爬取的數據以下圖python
源代碼:web
#-*-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)