抓取PPT網站上的資料
目標位置:
https://www.ptt.cc/bbs/Gossiping/index.html
- 先把html全抓回來
- 紀錄一個cookie是否年滿18歲的變數
- 套件抓取今天日期, 去掉開頭的 '0' 符合 PTT 網站格式
- 取得上一頁href
- 抓取所有文章
- 判斷文章是否是今天發佈
- 取得推文數
- 取得文章連結
- 取得標題
- 取得作者
- 把我們要的資料整理好後,存入一開始的空陣列 後回傳(return)回去
先把html全抓回來
import requests
domain_url = 'https://www.ptt.cc'
def get_ppt_page(url):
resp = requests.get(
url=url,
)
if resp.status_code != 200:
print('Invalid url:', resp.url)
return None
else:
return resp.text
if __name__ == '__main__':
ppt_page = get_ppt_page(domain_url + '/bbs/Gossiping/index.html')
if ppt_page:
print(domain_url + '/bbs/Gossiping/index.html')
print("目前有文章")
紀錄一個cookie是否年滿18歲的變數
cookies={'over18': '1'} # 紀錄cookies 是否年滿18歲
import requests
domain_url = 'https://www.ptt.cc'
def get_ppt_page(url):
resp = requests.get(
url=url,
cookies={'over18': '1'} # 紀錄cookies 是否年滿18歲
)
if resp.status_code != 200:
print('Invalid url:', resp.url)
return None
else:
return resp.text
if __name__ == '__main__':
ppt_page = get_ppt_page(domain_url + '/bbs/Gossiping/index.html')
if ppt_page:
print(domain_url + '/bbs/Gossiping/index.html')
print("目前有文章")
#取得今天日期, 去掉開頭的 '0' 符合 PTT 網站格式
import time
today = time.strftime("%m/%d").lstrip('0') # 今天日期, 去掉開頭的 '0' 以符合 PTT 網站格式
print(today)
取得上一頁href

import requests
import time
from bs4 import BeautifulSoup
domain_url = 'https://www.ptt.cc'
def get_ppt_page(url):
resp = requests.get(
url=url,
cookies={'over18': '1'} # 紀錄cookies 是否年滿18歲
)
if resp.status_code != 200:
print('Invalid url:', resp.url)
return None
else:
return resp.text
def get_pageinfo(resdata):
soup = BeautifulSoup(resdata, 'html5lib')
#取得上一頁href
paging_div = soup.find('div', 'btn-group btn-group-paging')
print(paging_div)
prev_url = paging_div.find_all('a')[1]['href']
print(prev_url)
return prev_url
if __name__ == '__main__':
ppt_page = get_ppt_page(domain_url + '/bbs/Gossiping/index.html')
if ppt_page:
#print(domain_url + '/bbs/Gossiping/index.html')
articles = [] # 全部的今日文章
today = time.strftime("%m/%d").lstrip('0') # 今天日期, 去掉開頭的 '0' 以符合 PTT 網站格式
prev_href = get_pageinfo(ppt_page)
print(domain_url+prev_href)


抓取所有文章
import requests
import time
from bs4 import BeautifulSoup
domain_url = 'https://www.ptt.cc'
def get_ppt_page(url):
resp = requests.get(
url=url,
cookies={'over18': '1'} # 紀錄cookies 是否年滿18歲
)
if resp.status_code != 200:
print('Invalid url:', resp.url)
return None
else:
return resp.text
def get_pageinfo(resdata,today):
soup = BeautifulSoup(resdata, 'html5lib')
#取得上一頁href
paging_div = soup.find('div', 'btn-group btn-group-paging')
#print(paging_div)
prev_url = paging_div.find_all('a')[1]['href']
#print(prev_url)
pptdata = [] # 儲存取得的文章資料
date_divs = soup.find_all('div', 'r-ent')
print(date_divs) #先抓取<div class="r-ent"></div>
return pptdata,prev_url
if __name__ == '__main__':
ppt_page = get_ppt_page(domain_url + '/bbs/Gossiping/index.html')
if ppt_page:
#print(domain_url + '/bbs/Gossiping/index.html')
articles = [] # 全部的今日文章
today = time.strftime("%m/%d").lstrip('0') # 今天日期, 去掉開頭的 '0' 以符合 PTT 網站格式
pptdata,prev_href = get_pageinfo(ppt_page,today)
print(domain_url+prev_href)
print(pptdata)

判斷文章是否是今天發佈
for d in date_divs:
today_data = d.find('div', 'date').text.strip() == today
print(today_data)
if today_data : #判斷文章是否是今天發佈
print("取得推文數")
如果是今天發佈的會回傳 true,反之false

def get_pageinfo(resdata,today):
soup = BeautifulSoup(resdata, 'html5lib')
#取得上一頁href
paging_div = soup.find('div', 'btn-group btn-group-paging')
#print(paging_div)
prev_url = paging_div.find_all('a')[1]['href']
#print(prev_url)
pptdata = [] # 儲存取得的文章資料
date_divs = soup.find_all('div', 'r-ent')
#print(date_divs) #先抓取<div class="r-ent"></div>
for d in date_divs:
today_data = d.find('div', 'date').text.strip() == today
print(today_data)
if today_data : #判斷文章是否是今天發佈
print("取得推文數")
return pptdata,prev_url
取得推文數

if today_data : #判斷文章是否是今天發佈
# 取得推文數
push_count = d.find('div', 'nrec').text
print(push_count)

推文數,可能是'爆'或 'X1', 'X7', ...

如果是爆,可以給一個特別數字、如果是x開頭的也可以給一個數字
若不是, 不做任何事,push_num 保持為 0
push_num = 0
if push_count:
try:
push_num = int(push_count) # 轉換字串為數字
except ValueError:
if push_count == '爆':
push_num = 00
elif push_count.startswith('X'):
push_num = 99
print(push_num)

取得文章連結
#判斷超連結是否存在

#取得文章連結
if d.find('a'): # 有超連結,代表文章存在
href = d.find('a')['href']
print(href)

取得標題
title = d.find('a').text
print("標題",title)
取得作者
author = d.find('div', 'author').text if d.find('div', 'author') else '' #作者有可能是空的
print("作者",author)

if d.find('a'): # 有超連結,代表文章存在
href = d.find('a')['href']
print("標題連結",href)
title = d.find('a').text
print("標題",title)
author = d.find('div', 'author').text if d.find('div', 'author') else '' #作者有可能是空的
print("作者",author)
把我們要的資料整理好後,存入一開始的空陣列 後回傳(return)回去
"標題"、"標題連結"、"作者"、"推文數"
pptdata.append({
'title': title,
'href': href,
'push_num': push_num,
'author': author
})

完整Code:
import requests
import time
from bs4 import BeautifulSoup
domain_url = 'https://www.ptt.cc'
def get_ppt_page(url):
resp = requests.get(
url=url,
cookies={'over18': '1'} # 紀錄cookies 是否年滿18歲
)
if resp.status_code != 200:
print('Invalid url:', resp.url)
return None
else:
return resp.text
def get_pageinfo(resdata,today):
soup = BeautifulSoup(resdata, 'html5lib')
#取得上一頁href
paging_div = soup.find('div', 'btn-group btn-group-paging')
#print(paging_div)
prev_url = paging_div.find_all('a')[1]['href']
#print(prev_url)
pptdata = [] # 儲存取得的文章資料
date_divs = soup.find_all('div', 'r-ent')
#print(date_divs) #先抓取<div class="r-ent"></div>
for d in date_divs:
today_data = d.find('div', 'date').text.strip() == today
#print(today_data)
if today_data : #判斷文章是否是今天發佈
# 取得推文數
push_count = d.find('div', 'nrec').text
print(push_count)
push_num = 0
if push_count:
try:
push_num = int(push_count) # 轉換字串為數字
except ValueError:
# 若轉換失敗,可能是'爆'或 'X1', 'X2', ...
# 若不是, 不做任何事,push_num 保持為 0
if push_count == '爆':
push_num = 00
elif push_count.startswith('X'):
push_num = 99
print("推文數",push_num)
# 取得文章連結
if d.find('a'): # 有超連結,代表文章存在
href = d.find('a')['href']
print("標題連結",href)
title = d.find('a').text
print("標題",title)
author = d.find('div', 'author').text if d.find('div', 'author') else '' #作者有可能是空的
print("作者",author)
pptdata.append({
'title': title,
'href': href,
'push_num': push_num,
'author': author
})
return pptdata,prev_url
if __name__ == '__main__':
ppt_page = get_ppt_page(domain_url + '/bbs/Gossiping/index.html')
if ppt_page:
#print(domain_url + '/bbs/Gossiping/index.html')
articles = [] # 全部的今日文章
today = time.strftime("%m/%d").lstrip('0') # 今天日期, 去掉開頭的 '0' 以符合 PTT 網站格式
pptdata,prev_href = get_pageinfo(ppt_page,today)
print(domain_url+prev_href)
print(pptdata)
Yiru@Studio - 關於我 - 意如