forked from mikeku1116/happycoding-python-scraper-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper_to_excel.py
More file actions
29 lines (20 loc) · 944 Bytes
/
scraper_to_excel.py
File metadata and controls
29 lines (20 loc) · 944 Bytes
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
from bs4 import BeautifulSoup
import requests
import openpyxl
from openpyxl.styles import Font
response = requests.get("https://www.inside.com.tw/tag/AI")
soup = BeautifulSoup(response.content, "lxml")
cards = soup.find_all("div", {"class": "post_list_item"})
results = []
for card in cards:
title = card.find("h3", {"class": "post_title"})
published = card.find("li", {"class": "post_date"})
results.append((title.getText().strip(),) + (published.getText().strip(),))
wb = openpyxl.Workbook() # 建立工作簿
sheet = wb.create_sheet("inside", 0) # 建立工作表
sheet.append(("文章標題", "發佈日期")) # 寫入欄位名稱
sheet.cell(row=1, column=1).font = Font(color="0000FF") # 顯示藍色字體顏色
sheet.cell(row=1, column=2).font = Font(color="0000FF") # 顯示藍色字體顏色
for result in results:
sheet.append(result) # 寫入爬取的資料
wb.save("inside.xlsx") # 儲存Excel檔案