我寫了一個腳本來抓取網站上的PHP文件中的數據。我編寫了腳本,只有當頁面上的當前數據與上一次從頁面抓取數據時發生了變化,它纔會輸出數據。該頁面確實需要驗證,這就是爲什麼我添加了PHPSESSID。這將允許查看頁面。 Cloudflare已實施urllib2繞過cloudflare
在訪問website.com之前檢查您的瀏覽器。
該過程是自動的。您的瀏覽器很快就會重定向到您請求的內容。 請允許長達5秒...
這已經打破了我的腳本,它無法從頁面抓取數據。我看着cfscrape,並不知道如何將它實現到我當前的腳本中以使其運行。如果任何人都可以提供非常棒的幫助!我討厭不得不伸出援手,但我需要儘快讓這個功能起作用。
#!/usr/bin/python
from time import sleep
import re
import hashlib
import urllib2
import winsound
def doMd5(data):
mdo = hashlib.md5()
mdo.update(data)
return mdo.hexdigest()
def doRequest():
try:
head = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
'Cookie': 'PHPSESSID=ldc1bp9mj7n4ocffvftm25te62'}
req = urllib2.Request('http://www.example.com/random/random.php', headers=head)
res = urllib2.urlopen(req)
data = res.read()
return data
except:
return "exception"
last_log = ""
while True:
data = doRequest()
if data == "exception" or data == "":
print "Exception!"
continue
new_hash = doMd5(data)
if new_hash != last_log:
print "New Data"
winsound.Beep(100, 80)
handler = open('data.html', 'a')
handler.write(data + '\n')
handler.close()
last_log = new_hash
else:
sleep(3)
print "Refreshing..."
continue
用[cfscape usage](https://github.com/Anorov/cloudflare-scrape#usage)替換所有的urllib用法...你所需要做的就是獲取網站內容,對不對? –