1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
import gspread from google.oauth2.service_account import Credentials
import requests from bs4 import BeautifulSoup import time
NEW_BOOK_URL = "https://www.books.com.tw/web/books_newbook/" book_list_get = [] def main():
book_list = book_types(NEW_BOOK_URL) for index,item in enumerate(book_list): print(f"({index+1}) {item['title']}") kind_no = int(input("輸入圖書分類:")) if kind_no >= 1 and kind_no <= len(book_list): get_books(kind_no, book_list[kind_no-1]['url'], book_list[kind_no-1]['title']) save_to_google_sheet() else: print("分類錯誤!")
def get_books(index, url, title): print(f"-- {index} {title} {url} --") book_url = f"{url}?o=5&v=1" try: resp = requests.get(book_url) except: resp = None
pages = 0 start_index = 1 if resp and resp.status_code == 200: soup = BeautifulSoup(resp.text, "html.parser") try: pages = int(soup.select_one(".cnt_page .page span").text) except: pages = 1 books_no = show_books(title, start_index, soup) start_index += books_no
if (pages > 1): for i in range(2, pages + 1): book_url = f"{url}?o=5&v=1&page={str(i)}" try: resp = requests.get(book_url) except: resp = None if resp and resp.status_code == 200: soup = BeautifulSoup(resp.text, "html.parser") books_no = show_books(title, start_index, soup) start_index += books_no
def show_books(type_title, start_index, soup): global book_list_get
books = soup.select(".wrap .item") for index,book in enumerate(books): title = book.select_one(".msg h4 a").text img_url = book.select_one(".cover")['src'] info = book.select_one(".info").text.split(',') author = info[0] publisher = info[1] publish_date = info[2].split(":")[1] price_box = book.select(".price strong") if len(price_box) == 1: special_discount = '100' special_price = price_box[0].text else: special_discount = price_box[0].text special_price = price_box[1].text
content = book.select_one(".txt_cont").text.strip().replace("\n", "").replace(" ", "")
list_data = [type_title, title, img_url, author, publisher, publish_date, special_discount, special_price, content] book_list_get.append(list_data) return len(books)
def book_types(book_url): book_list = [] try : resp = requests.get(book_url) except: resp = None
if resp and resp.status_code == 200: soup = BeautifulSoup(resp.text, "html.parser")
types = soup.select(".mod_b li a") for type in types: book_list.append( { 'title': type.text, 'url': type['href'].split("?")[0] } ) return book_list
def save_to_google_sheet(): global book_list_get
scopes = [ "https://www.googleapis.com/auth/spreadsheets" ]
creds = Credentials.from_service_account_file("tutorial-sheets-421106-4878ffb056ac.json", scopes=scopes) client = gspread.authorize(creds) sheet_id = '1m6s3HXmc6vtVuvbgF5MytNIGB1BgdnovDWdqT7pJZJs'
workbook = client.open_by_key(sheet_id)
print("save books to google sheet..") sheet = workbook.get_worksheet(0) sheet.clear() list_title = ["分類", "書名", "圖片網址", "作者", "出版社", "出版日期", "折扣", "優惠價", "內容"] sheet.append_row(list_title) for item in book_list_get: sheet.append_row(item) time.sleep(1.2)
if __name__ == '__main__': main()
|