Selenium应用(一)

from selenium import webdriver

def get_url_list():

  #设置浏览器后台运行
  chrome_options = webdriver.ChromeOptions()
  chrome_options.add_argument('--headless')

  browser = webdriver.Chrome(chrome_options=chrome_options)
  url='https://fgo.wiki/w/%E8%8B%B1%E7%81%B5%E5%9B%BE%E9%89%B4'
  browser.get(url)
  browser.implicitly_wait(3)

  #模拟点击标签展开列表
  select = browser.find_element_by_id('per-page')
  select.find_element_by_xpath('option[4]').click()

  table = browser.find_element_by_id('lancelot_table_servantlist')
  trs = table.find_elements_by_xpath('./tbody/tr')

  url_list = []
  for tr in trs[1:]:
    url = tr.find_element_by_xpath('td[2]/a').get_attribute('href')
    url_list.append(url)

  #关闭进程应用.quit() .close()是关闭浏览器
  browser.quit()
  return  url_list

lists = get_url_list()
for list in lists:
  print(list)


   转载规则


《Selenium应用(一)》 刘坤胤 采用 知识共享署名 4.0 国际许可协议 进行许可。
  目录