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