Advertisement
jackpieno

Beautiful Soup test

May 8th, 2018
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. from selenium import webdriver
  2. from bs4 import BeautifulSoup
  3.  
  4.  
  5. def savefile(filename,con): #存取檔案
  6.     fp = open(filename,"w",encoding="utf-8")
  7.     fp.writelines(con)
  8.     fp.close()
  9.  
  10.  
  11. browser = webdriver.Chrome()
  12. browser.get("https://e-dictionary.apc.gov.tw/bnn/Search.htm")
  13. browser.find_element_by_link_text("詞項列表").click()#webdriver定位用 找到網址上的詞項列表並點擊
  14. browser.find_element_by_xpath("//a[@rel='287890']").click()#用webdriver 絕對路徑功能找到 "//a[@rel='287890']"的程式碼並點擊
  15. soup = BeautifulSoup(browser.page_source,"lxml")#建立一個soup 存入webdriver的網址程式碼
  16. tag = soup.find(rel="287890") #使用BeautifulSoup 的find()方法 找aabuhan的特徵  特徵為rel="287890"
  17. savefile("test1.txt",soup)#建立test1.txt檔 存入soup資料
  18. savefile("test2.txt",tag)#建立test2.txt檔 存入tag資料
  19. print(tag)#印出程式碼
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement