Created
August 4, 2019 13:47
-
-
Save ryuichimatsumoto-single/e59b78d910027e35f7801210565c1eed to your computer and use it in GitHub Desktop.
mercari webscraping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from selenium import webdriver | |
driver = webdriver.Chrome("c:/chromedriver.exe") | |
driver.get("https://www.mercari.com/jp/search/?keyword=XXXXXX") | |
# Excelで読み込めるようにSJISで保存 | |
f = open('C:\\Users\\matsumoto\\Documents\\result.csv', 'w',encoding='CP932', errors='replace') | |
# csvファイルを定義 | |
writer = csv.writer(f, lineterminator="\n") | |
# csvに書き込む内容 | |
list = [] | |
i = 1 | |
while i < 90: | |
# 商品タイトルを取得、取れない場合はループ終了 | |
try: | |
a = driver.find_element_by_xpath("/html/body/div[1]/main/div[1]/section/div[2]/section["+ str(i) +"]/a/div/h3").text.replace('\xa5', '').replace('\n', '') | |
except: | |
break | |
# 販売中か販売終了かを取得 | |
try: | |
b = driver.find_element_by_xpath("/html/body/div[1]/main/div[1]/section/div[2]/section["+ str(i) +"]/a/figure/figcaption/div/div").text | |
except: | |
b = "SELL" | |
# 価格を取得 | |
c = driver.find_element_by_xpath("/html/body/div[1]/main/div[1]/section/div[2]/section["+ str(i) +"]/a/div/div/div").text.replace('\xa5', '').replace('\n', '').replace(',', '') | |
# csv書き込み用の配列に追加 | |
list.append([a,b,c]) | |
i = i + 1 | |
# 調査結果を書き込む | |
writer.writerows(list) | |
# ファイル、ブラウザを閉じる | |
f.close() | |
driver.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment