ウェブサイトにログインしてデータを取得し、ファイルに保存します。しばらくしてから新しいデータを取得し、古い(保存された)データと比較して、何か変更があった場合は印刷します。それ、どうやったら出来るの?ログインは機能していますが、比較は機能していません。どうして?
前もって感謝します!
私のコード:
# -*- coding: utf-8 -*-
import urllib
import urllib2
import cookielib
import time
def login():
username = "username"
password = "password"
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'login_username' : username, 'login_password' : password})
opener.open('lol.com/login', login_data)
resp = opener.open('lol.com/login')
data = resp.read()
print data
write_data(data)
def write_data(data):
file = open("htmlString", "w")
file.write(data)
file.close()
monitor(data)
def monitor(data):
string1 = open("htmlString", "r").read()
string2 = data
while True:
time.sleep(5)
login()
if string1 == string2:
print "Nothing has changed"
else:
print "Something has changed"
login()
私はあなたのコードに従うのがかなり難しいと思いました。これは、達成しようとしていることに近づくはずのテストされていない代替案です。
def fetch_html():
# fetch logic
return html # string
def write_html(html): # string
# write logic
def read_html():
with open('page.html','r') as f:
return f.read()
def monitor():
write_html(fetch_html())
while True:
time.sleep(5)
new_html = fetch_html()
if new_html == read_html():
print('Nothing has changed')
else:
print('Something has changed')
write_html(new_html)
monitor()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加