SSブログ

スクレイピングに挑戦 [python]

Webスクレイピーング

import urllib.request, urllib.error
from bs4 import BeautifulSoup

url = "https://www.kadokawa.co.jp/product/search/?lgenre=13&releaseDate=1"
#python2系ではurllib.request(url)
html = urllib.request.urlopen(url)
soup = BeautifulSoup(html, "html.parser")

h2 = soup.find_all("h2")

book_titles = []
for tag in h2:
    try:
        string_ = tag.get("class").pop(0)
        if string_ in "book-title":
            ts = tag.string.replace(' ', ' ')
            book_titles.append(ts)
            #break
    except:
        pass
print(book_titles)
with open('titles.csv', mode='w+',encoding='utf-8') as f:
    for i in book_titles:
        s = f.write('%s,' %i)


nice!(0)  コメント(0) 

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。