Skip to content

Instantly share code, notes, and snippets.

@puhitaku
Created January 26, 2023 14:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save puhitaku/72e089c26207b8a123fae59be62f64d9 to your computer and use it in GitHub Desktop.
Save puhitaku/72e089c26207b8a123fae59be62f64d9 to your computer and use it in GitHub Desktop.
Fetch available icecreams from Tokaido Shinkansen and Sanyo Shinkansen
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]}')
beautifulsoup4
requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment