#! python3 # downloadXkcdy.py - XKCDコミックを一つずつダウンロードする import requests, os, bs4 url = 'http://xkcd.com' # 開始URL os.makedirs('xkcd', exist_ok=True) # ./xkcdに保存する while not url.endwidth('#'): # ページをダウンロードする print('ページをダウンロード中{}...'.format(url)) res = requests.get(url) res.raise_for_status() soup = bs4.BeautifulStoneSoup(res.text) # コミック画像のURLを見つける comic_elem = soup.select('#comic img') if comic_elem == []: print('コミック画像が見つかりませんでした') else: comic_url = 'http:' + comic_elem[0].get('src') # 画像をダウンロードする print('画像をダウンロード中{}...'.format(comic_url)) res = requests.get(url) res.raise_for_status() # TODO: # 画像を./xkcdに保存する # TODO: # prevボタンのURLを取得する
# build request Header #### important #### request = req.Request( url, headers={ "cookie": "over18=1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36" }) #open web as request with req.urlopen(request) as response: data = response.read().decode("utf-8") import bs4 root = bs4.BeautifulStoneSoup(data) # 標題 titles titles = root.find_all("div", class_="title") # find title for title in titles: if title.a != None: print(title.a.string) # 日期 dates = root.find_all("div", class_="date") # find for date in dates: print(date.string) # 作者 authors = root.find_all("div", class_="author") # find for author in authors:
def getEachPage(self, html): #soup = BeautifulSoup(html) soup1 = bs4.BeautifulStoneSoup(html) paimai = soup1.findChildren(bgcolor="#ffffff") return paimai