(^・ω・^)丿
結果我睡不著爬起來寫code…
想趕快讓大家脫離苦難
您檢查一下看看有沒有錯誤…
我也不確定精神不濟實寫出來的code到底對不對
我是能run,但沒完全測試..
我寫了執行先輸入編號,因為我不覺得每個人都會開cmd然後輸入參數
另外除了status code 200以外就休息,
順便抓完會在log,txt裡面插一筆紀錄,
免得視窗當掉甚麼都看不見。
有的功能我覺得能加上去會更棒,
但沒有也沒關係就是了…
因為我沒辦法pull您的Github
希望貼在這邊希望排版不會跑掉…
初學者寫的code,請見諒><
import sys
import requests
import time
from bs4 import BeautifulSoup
def hello():
ticks = str(time.strftime("%m-%d-%H:%M:%S", time.localtime()))
print("請輸入要從第幾篇開始抓(請輸入數字:例如10000)")
n = input()
print("請輸入到第幾篇結束(請輸入數字:例如10001的話只會抓1篇)")
n2 = input()
#這邊開始請做非數字的判定...MaoMaru
#if n != int:
# return
#if n2 != int:
# return
#以上結束
i2=0
for i in range(int(n), int(n2)):
print("[" + ticks + "]開始抓取第: " + str(i) + "篇")
headers = { 'accept':'*/*',
'accept-encoding':'gzip, deflate, br',
'accept-language':'en-GB,en;q=0.9,en-US;q=0.8,hi;q=0.7,la;q=0.6',
'cache-control':'no-cache',
'dnt':'1',
'pragma':'no-cache',
'referer':'https',
'sec-fetch-mode':'no-cors',
'sec-fetch-site':'cross-site',
'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
}
Html = 'http://webcache.googleusercontent.com/search?q=cache:https://kater.me/d/' + \
str(i)
res = requests.get(Html, headers=headers, timeout=30)
res.encoding = "utf-8"
print("[" + ticks + "]" + "[Debug]Status Code: " + str(res.status_code))
if res.status_code != 200:
time.sleep(15) #這邊也許可以使用隨機
print("[" + ticks + "]" + "休息15秒")
continue
else:
soup = ""
soup = BeautifulSoup(res.text, 'html.parser')
with open(str(i) + '.html', 'w', encoding="utf-8") as file:
file.write(soup.prettify())
i2 += 1
print("[" + ticks + "]" + "抓完" + str(i2) + "篇囉,休息1分鐘~")
with open('log.txt', 'w', encoding="utf-8") as file:
file.write("[" + ticks + "]" + "已抓了" + str(i2) + "篇文章")
time.sleep(60) #這邊也許可以使用隨機
hello()