Created
January 26, 2023 14:04
-
-
Save puhitaku/09d4b5c33f136e191935ff8cb38ef292 to your computer and use it in GitHub Desktop.
Fetch available icecreams from Tokaido Shinkansen and Sanyo Shinkansen
This file contains hidden or 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 requests as req | |
from bs4 import BeautifulSoup | |
def scrape_tokai() -> list[tuple]: | |
res = req.get('https://www.jr-cp.co.jp/wp-json/wp/v2/services?per_page=100&services_category=21') | |
j = res.json() | |
return [(item['title']['rendered'], item['data']['price_services']) for item in res.json()] | |
def scrape_sanyo() -> list[tuple]: | |
res = req.get('https://www.jwfsn.com/business/shinkansen_service/wagon/') | |
soup = BeautifulSoup(res.text, features='html.parser') | |
icecreams = soup.find(class_='blk-title', string='アイスクリーム').find_next_sibling().find_all('p') | |
items = [] | |
for i in icecreams: | |
tokens = list(i.stripped_strings) | |
items.append((tokens[0], tokens[1].replace('円', ''))) | |
return items | |
tokai = scrape_tokai() | |
print('東海道新幹線') | |
for item in tokai: | |
print(f'- {item[0]} ¥{item[1]}') | |
print() | |
sanyo = scrape_sanyo() | |
print('山陽新幹線') | |
for item in sanyo: | |
print(f'- {item[0]} ¥{item[1]}') |
This file contains hidden or 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
beautifulsoup4 | |
requests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment