カテゴリページからタグを取得して一覧にします(URLダブりあり。はてな記法)
各カテゴリページの1ページ目だけ対応。
Headless Chromeで動かしてみました。
参考:Windows環境でSeleniumからHeadless Chromeを動かしてみた | Developers.IO
#よくわかってない from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.chrome.options import Options from bs4 import BeautifulSoup import urllib.parse options = Options() options.binary_location = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe' options.add_argument('--headless') #タグをゲットするカテゴリ setTags=['C#','Excel','JavaScript','jQuery','Outlook','php','PowerPoint','python3','vba','Word','FC東京'] with open(r"C:\Users\user\Documents\tagLog.txt", mode='w') as f: f.write("|*No|*Tag|*Entry|\n") #ログイン画面設定フラグ loginFig=False #タグ設定画面初回フラグ fastTaguSet=False #chrome開くよ with webdriver.Chrome(options=options) as driver: #よくわかってない wait = WebDriverWait(driver, 10) i=1 for setTag in setTags: #カテゴリページのURL driver.get("https://snack.elve.club/archive/category/" + urllib.parse.quote(setTag)) print("==="+setTag+"===") WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located)#読み込み待ち #記事の一覧画面 html = driver.page_source.encode('utf-8') #解析用 soup = BeautifulSoup(html, 'lxml') entrys=soup.find_all("section", class_="archive-entry test-archive-entry autopagerize_page_element") for entry in entrys: URL=entry.find("a", class_="entry-title-link") tag = entry.find("div", class_="archive-entry-tag-label") if tag!=None: f.write("|" + str(i) + "|#"+setTag+"|"+"[" + URL["href"] + ":title]|\n") i+=1
出力結果(記事重複あり)
各タグでちょっと邪魔な感じになってると思います。すみませんすみません(-_-;)
グリーンスター100個!ヾ(o´∀`o)ノワァーィ♪wwwww