前段时间, 领导说列一下某页面上的所有国家信息, 话说这个国家下拉框里的国家有两三百个, 是第三方模块导入的, 手动从页面拷贝, 不切实际, 于是想着用爬虫去获取这个国家信息, 并保存到文件里
下面是具体的代码, 写的也是比较简单, 利用 Selenium 操作页面, 获取下拉国家列表的信息, 并保存到文件里
- from selenium import webdriver
- import xlwt
- driver = webdriver.Chrome(C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe)
- driver.get(https://xxx.xx.com/contact)
- countries = driver.find_element_by_id(country)# 获取 country dropdownlist
- options_list = countries.find_elements_by_tag_name(option)
- options = []
- for option in options_list: #获取 country dropdownlist 里所有的数据
- options.append(option.text)
- with open(country_list.txt,w+, encoding=utf-8) as f:
- for i in options:
- if i != Select your country:
- f.write(i+\n)
- driver.quit()
来源: http://www.bubuko.com/infodetail-2499066.html