Skip to content

Instantly share code, notes, and snippets.

@allieus
Last active June 14, 2019 01:19
Show Gist options
  • Select an option

  • Save allieus/c374d78ee624541af10450ba4431138e to your computer and use it in GitHub Desktop.

Select an option

Save allieus/c374d78ee624541af10450ba4431138e to your computer and use it in GitHub Desktop.
import re
import requests
from itertools import count
from pathlib import Path
from bs4 import BeautifulSoup
from clint.textui import progress
def get_list(pid):
for page in count(1):
print('try page {}'.format(page))
url = 'http://www.podbbang.com/podbbangchnew/episode_list?id={pid}&page={page}'.format(pid=pid, page=page)
response = requests.get(url)
response.encoding = 'utf8'
html = response.text
# soup = BeautifulSoup(html, 'html.parser')
soup = BeautifulSoup(html, 'lxml')
for dl_tag in soup.select('li > dl'):
try:
title = dl_tag.find('dt')['title']
js = dl_tag['onclick']
matched = re.search(r"'(\d+)',\s*'(\w+/\w+)'", js)
if matched:
eid, content_type = matched.groups()
mp3_download(pid, eid, title)
except KeyError:
print('Ended')
return None
line = input('{page} 페이지를 시도할까요? (Y/n) '.format(page=page+1)).strip().lower()
if line and not line.startswith('y'):
print('Ended')
break
def mp3_download(pid, eid, title):
url = 'http://www.podbbang.com/download?pid={pid}&eid={eid}'.format(pid=pid, eid=eid)
headers = {
'Referer': 'http://www.podbbang.com/ch/{pid}'.format(pid=pid),
}
r = requests.get(url, headers=headers, stream=True)
if r.status_code == 200:
filepath = Path('{}.mp3'.format(title))
total_length = int(r.headers.get('content-length'))
if filepath.exists() and filepath.stat().st_size == total_length:
print('{} - 이미 다운받았습니다.'.format(title))
else:
print('{} - 다운로드'.format(title))
with filepath.open('wb') as f:
chunk_size = 1024
expected_size = (total_length//chunk_size) + 1
for chunk in progress.bar(r.iter_content(chunk_size=chunk_size), expected_size=expected_size):
f.write(chunk)
else:
print('download failed. status code = {}'.format(r.status_code))
if __name__ == '__main__':
get_list('12548')
beautifulsoup4
requests
clint
@allieus
Copy link
Author

allieus commented Mar 5, 2017

위 스크립트 실행을 위해, 3가지 라이브러리가 필요합니다. 아래와 같이 개별로 설치하셔도 되구요.

pip install beautifulsoup4 requests clint

위 목록을 파일에 저장하시고, 아래와 같이 명령을 하셔도 됩니다.

pip install -r requirements.txt

@ymkim92
Copy link

ymkim92 commented Jun 14, 2019

안녕하세요 @allieus 님,

여기 올려주신 내용보고 저도 뭘 좀 해 보았는데요. 한 가지 궁금한 것이 있어서 연락드립니다.
url = 'http://www.podbbang.com/podbbangchnew/episode_list?id={pid}&page={page}'.format(pid=pid, page=page)
위 url은 일반에게 공개된 url이 아닌 것 같습니다. 제 말이 맞나요? 그렇다면, 어떻게 이 url을 알아내시게 되었는지 궁금합니다.

제가 비슷한 걸 만들어 보려고 했을때 팟빵 메인 페이지를 통해서 접근했습니다. 나중에 이 url을 알고 나서 에피소드들에 접근하기가 훨씬 쉬운 것을 알고 좀 놀랐습니다. 어떻게 이렇게 접근하기 편리한 url을 찾으셨나요?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment