Selenium 第五课作业 - 访问网址获取前10页的股票名称和代码,并且保存到文件中
题目
访问网址:http://quote.eastmoney.com/stock_list.html,获取前10页的股票 名称和代码,并且保存到文件中
参考代码
点击查看代码
import json
from time import sleep
from selenium import webdriver
stock_dict = {}
wd = webdriver.Chrome(r'C:\Program Files\Google\Chrome\Application\chromedriver.exe')
wd.implicitly_wait(30)
wd.get('http://quote.eastmoney.com/stock_list.html')
# 获取页数
page = wd.find_element_by_css_selector('.paginate_button.current').text
while int(page) <= 10:
tables = wd.find_elements_by_css_selector('tbody tr')
for table in tables:
stock = table.find_elements_by_css_selector('td:nth-child(2),td:nth-child(3)')
stock_dict[stock[1].text] = stock[0].text
# 点击下一页
wd.find_element_by_css_selector('.paginate_button.current + a').click()
# 等待刷新,判断目前页数
sleep(2)
page = wd.find_element_by_css_selector('.paginate_button.current').text
wd.quit()
with open('stock.txt','w',encoding='utf8') as f:
f.write(json.dumps(stock_dict,indent=4,ensure_ascii=False))

浙公网安备 33010602011771号