from selenium import webdriver
import time
import pandas
from webdriver_manager.chrome import ChromeDriverManager
#キーワード入力
search_word = input("検索キーワード=")
メルカリ
url = 'https://www.mercari.com/jp/search/?keyword=' + search_word
chromedriverの設定とキーワード検索実行
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.set_window_size(1200,1000)
driver.get(url)
ページカウントとアイテムカウント用変数
page = 1
item_num = 0
item_urls = []
while True:
print("Getting the page {} ...".format(page))
time.sleep(1)
items = driver.find_elements_by_class_name("ItemGrid__ItemGridCell-sc-14pfel3-1")
for item in items:
item_num += 1
item_url = item.find_element_by_css_selector("a").get_attribute("href")
print("item{0} url:{1}".format(item_num, item_url))
item_urls.append(item_url)
page += 1
try: next_page = driver.find_element_by_css_selector("li.pager-next .pager-cell:nth-child(1) a").get_attribute("href") driver.get(next_page) print("next url:{}".format(next_page)) print("Moving to the next page...") except: print("Last page!") break
アイテムカウントリセットとデータフレームセット
item_num = 0
columns = ["item_name", "cat1", "cat2", "cat3", "brand_name", "product_state", "price", "url"]
df = pandas.DataFrame(columns=columns)
try: # エラーで途中終了時をtry~exceptで対応
# 取得した全URLを回す
for product_url in item_urls:
item_num += 1
print("Moving to the item {}...".format(item_num))
time.sleep(1)
driver.get(product_url)
time.sleep(2) item_name = driver.find_element_by_xpath("//mer-heading[@class='mer-spacing-b-2']").get_attribute("title-label") print("Getting the information of {}...".format(item_name)) cat = driver.find_elements_by_xpath("//a[@data-location='item:item_detail_table:link:go_search']") cat1 = cat[0].accessible_name cat2 = cat[1].accessible_name cat3 = cat[2].accessible_name try: # 存在しない⇒a, divタグがない場合をtry~exceptで対応 brand_name = driver.find_element_by_css_selector("table.item-detail-table tbody tr:nth-child(3) td a div").text except: brand_name = "" product_state = driver.find_element_by_xpath("//span[@data-testid='商品の状態']").text price = driver.find_element_by_xpath("//mer-price").get_attribute("value") price = price.replace("¥", "").replace(" ","").replace(",", "") print(cat1) print(cat2) print(cat3) print(brand_name) print(product_state) print(price) print(product_url) se = pandas.Series([item_name, cat1, cat2, cat3, brand_name, product_state, price, product_url], columns) df = df.append(se, ignore_index=True) print("Item {} added!".format(item_num))
except:
print("Error occurred! Process cancelled but the added items will be exported to .csv")
#df.to_csv("{}.csv".format(search_word), index=False, encoding="utf_8")
df.to_csv("{}.csv".format(search_word), index=False, encoding="utf_8_sig")
driver.quit()
print("Scraping is complete!")
先日、サイトをみながらこのようなコードを模写していたのですが、サイトの構造が変わってしまい、もう一度同じようなプログラムを作りたいのですが、Webサイトの検証モードの使い方に慣れていなくて、いちいちどこの要素を指しているのかわかりません。
初心者にもわかりやすく教えて頂きたいです。
まず、こちらがどの要素を抜き出しているのかわかりません。
driver.find_elements_by_class_name("ItemGrid__ItemGridCell-sc-14pfel3-1")
次にこのby css_selectorでの特定もよくわかりません。
item.find_element_by_css_selector("a").get_attribute("href")
