Skip to content

Instantly share code, notes, and snippets.

@tamago324
Created October 30, 2017 22:06
Show Gist options
  • Save tamago324/6f38ddd9f559eb5954cd03252d039e54 to your computer and use it in GitHub Desktop.
Save tamago324/6f38ddd9f559eb5954cd03252d039e54 to your computer and use it in GitHub Desktop.
ファミマのキャンペーンを表示(BeautifulSoup4使用)
#!/bin/usr/env python
'''
ファミマでいま開催しているキャンペーンを取得するプログラム
http://www.family.co.jp/campaign.html
'''
import requests
from bs4 import BeautifulSoup
def scrape_campaigns():
'''
キャンペーンの取得
'''
# HTMLの取得
url = ('http://www.family.co.jp/campaign.html')
html = requests.get(url).text
# BeautifulSoupオブジェクトの生成
soup = BeautifulSoup(html, 'html.parser')
camps = []
for camp in soup.select('.ly-mod-infoset5-link'):
# URLは少し加工する
if camp['href'].startswith('/'):
url = 'http://www.family.co.jp' + camp['href']
else:
url = camp['href']
camps.append({
'title': camp.find(class_='ly-mod-infoset5-ttl').text,
'detail': camp.find(class_='ly-mod-infoset5-txt').text,
'period': camp.find(class_='ly-mod-infoset5-notes').text,
'url': url,
})
return camps
def print_campaignss(camps):
'''
キャンペーンを表示する
'''
for i, camp in enumerate(camps, start=1):
print('-------------------')
print_str = (
f'No.{i}\n'
f'{camp["title"]}\n'
f'{camp["detail"]}\n'
f'{camp["period"]}\n'
f'{camp["url"]}'
)
print(print_str)
def main():
camps = scrape_campaigns()
print_campaignss(camps)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment