|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下- from selenium import webdriver
- from selenium.webdriver.chrome.options import Options
- import time
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.ui import WebDriverWait #等待一个元素加载完成
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.common.action_chains import ActionChains#引入鼠标操作
- hot_stock_concept={}
- hot_stock_name={}
- def creat_name_dic():
- #这个是一个用来控制chrome以无界面模式打开的浏览器
- #创建一个参数对象,用来控制chrome以无界面的方式打开
- chrome_options = Options()
- #后面的两个是固定写法 必须这么写
- chrome_options.add_argument('--headless')
- chrome_options.add_argument('--disable-gpu')
- #驱动路径 谷歌的驱动存放路径
- path = r'C:\Users\Administrator\Desktop\py练习\乐\爬虫专\chromedriver.exe'
- #创建浏览器对象
- browser = webdriver.Chrome(executable_path=path,chrome_options=chrome_options)
- url ='http://quote.eastmoney.com/center/boardlist.html#concept_board'
- browser.get(url)
- time.sleep(1)
- stock_name = browser.find_elements_by_xpath("//td[contains(@class,'mywidth3')]/a")
- #写入字典
- k=1
- a=1
- b=1
- global hot_stock_concept,hot_stock_name
- for i in stock_name:
- if k%2!=0:#用取余的方法来区分奇偶
- hot_stock_concept[i]=a
- a=a+1
- else:
- hot_stock_name[i]=b
- b=b+1
- k=k+1
-
- print(hot_stock_concept)
-
- browser.quit()
-
- if __name__ =='__main__':
- creat_name_dic()
复制代码
就会这样{<selenium.webdriver.remote.webelement.WebElement (session="e45d0ac7c4f84ebaa16e75a48e0e622a", element="0.647855798634275-1")>: 1, <selenium.webdriver.remote.webelement.WebElement (session="e45d0ac7c4f84ebaa16e75a48e0e622a", element="0.647855798634275-3")>: 2,
我想要字典能够以中文显示出来,请问大佬们是需要怎么写...
- stock_name = browser.find_elements_by_xpath("//td[contains(@class,'mywidth3')]/a/text()")
复制代码
|
|