Mac-Python 零基础爬虫学习笔记(4):爬虫基金数据

# coding: utf-8
from selenium import webdriver
import time
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

driver = webdriver.Firefox(executable_path= '/Users/Mike/Downloads/geckodriver')

driver.get('http://fundf10.eastmoney.com/jjjz_160140.html')
i = 1
while i < 5:
    i = i + 1
    driver.find_element_by_xpath('//label[@value={}]'.format(i)).click()
    time.sleep(5)
    trs = driver.find_elements_by_xpath('//table[@class="w782 comm lsjz"]//tr')
    for tr in trs:
        ths = tr.find_elements_by_xpath('./td')
        with open('test.csv','a') as f:
            for th in ths:
                f.write(th.text.replace('\n', '\r\r') + ',' if th.text else '')
            f.write('\n')
driver.quit()

猜你喜欢

转载自blog.csdn.net/milkbusy/article/details/81749927
今日推荐