niihama-premium.com
import requests
import csv
from bs4 import BeautifulSoup
url = "https://niihama-premium.com/citizen/shop.html"
headers = {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
}
r = requests.get(url, headers=headers)
if r.status_code == requests.codes.ok:
soup = BeautifulSoup(r.content, "html5lib")
with open("result.csv", "w") as fw:
writer = csv.writer(fw, dialect="excel", lineterminator="\n")
writer.writerow(["業種", "店舗名", "住所", "TEL", "備考"])
for i in soup.select("#shop01 > div > section"):
genre = i.select_one("h4 > span").get_text(strip=True)
for tr in i.select("div.list_area > table.shop_list > tbody > tr"):
if tr.td:
data = [td.get_text(strip=True) for td in tr.select("td")]
writer.writerow([genre] + data)