मैं अजगर के लिए नया हूं और मुझे समझ में नहीं आता कि यह क्यों काम नहीं कर रहा है, लेकिन मैंने इस मुद्दे को कोड की एक पंक्ति तक सीमित कर दिया है।
इस बॉट का उद्देश्य एक वेबसाइट से HTML को परिमार्जन करना है (टेक्स्ट बदलने पर सुंदर और पोस्ट टू डिसॉर्डर का उपयोग करना। मैं FC2 और FR2 (flightcategory2 और flightrestrictions2) का उपयोग मेमोरी वैरिएबल के रूप में करता हूं ताकि कोड हर बार चलने के खिलाफ जांच कर सके। यदि वे वही हैं, कोड _ मिनट के लिए प्रतीक्षा करता है और फिर से जांचता है, अगर वे अलग हैं तो यह इसे पोस्ट करता है।
हालाँकि, इस कोड को चलाते समय, "flightCategory" "flightRestrictions" चर पहली बार कोड चलने पर बदल जाते हैं, लेकिन किसी कारण से वेबसाइट पर HTML टेक्स्ट बदलने पर बदलना बंद हो जाता है। विचाराधीन रेखा यह है यदि लूप।
if 1==1: # using 1==1 so this loop constantly runs for testing, otherwise I have it set for a time
flightCategory, flightRestrictions = und.getInfo()
जब डिबगिंग मोड, कोड चलता है, लेकिन कोड में चर अपडेट नहीं होते हैं, और मैं उलझन में हूं कि वे पहली बार कोड चलाने पर अपडेट क्यों करेंगे, लेकिन अनुक्रमिक समय नहीं। यह लाइन मेरे कोड के संचालन के लिए महत्वपूर्ण है।
इसे पढ़ने में आसान बनाने के लिए कोड का संक्षिप्त संस्करण यहां दिया गया है। मैं किसी भी मदद की सराहना करता हूं।
FC2 = 0
FR2 = 0
flightCategory = ""
flightRestrictions = ""
class UND:
def __init__(self):
page = requests.get("http://sof.aero.und.edu")
self.soup = BeautifulSoup(page.content, "html.parser")
def getFlightCategory(self): # Takes the appropriate html text and sets it to a variable
flightCategoryClass = self.soup.find(class_="auto-style1b")
return flightCategoryClass.get_text()
def getRestrictions(self): # Takes the appropriate html text and sets it to a variable
flightRestrictionsClass = self.soup.find(class_="auto-style4")
return flightRestrictionsClass.get_text()
def getInfo(self):
return self.getFlightCategory(), self.getRestrictions()
und = UND()
while 1 == 1:
if 1==1: #using 1==1 so this loop constantly runs for testing, otherwise I have it set for a time
flightCategory, flightRestrictions = und.getInfo() (scrape the html from the web)
if flightCategory == FC2 and flightRestrictions == FR2: # if previous check is the same as this check then skip posting
Do Something
elif flightCategory != FC2 or flightRestrictions != FR2: # if any variable has changed since the last time
FC2 = flightCategory # set the comparison variable to equal the variable
FR2 = flightRestrictions
if flightRestrictions == "Manager on Duty:": # if this is seen only output category
Do Something
elif flightRestrictions != "Manager on Duty:":
Do Something
else:
print("Outside Time")
time.sleep(5) # Wait _ seconds. This would be set for 30 min but for testing it is 5 seconds. O
1 उत्तर
आपके कोड के अनुसार, आप केवल http://sof.aero.und.edu
को एक अनुरोध भेज रहे हैं जब आप UND वर्ग का उदाहरण बना रहे हों। इसलिए, लूप के दौरान आपके उदाहरण की सूप विशेषता कभी भी अपडेट नहीं होती है और आपको पुराने मान मिलते रहते हैं।
आप इसे निम्नलिखित तर्क के साथ काम कर सकते हैं:
class UND:
def __init__(self):
pass
def scrape(self):
page = requests.get("http://sof.aero.und.edu")
self.soup = BeautifulSoup(page.content, "html.parser")
## SOME CODE
und = UND()
while 1 == 1:
und.scrape() # We scrape the website at the beginning of each loop iteration
## SOME OTHER CODE
संबंधित सवाल
नए सवाल
python
पायथन एक बहु-प्रतिमान है, गतिशील रूप से टाइप किया हुआ, बहुउद्देशीय प्रोग्रामिंग भाषा है। यह एक साफ और एक समान वाक्यविन्यास सीखने, समझने और उपयोग करने के लिए त्वरित होने के लिए डिज़ाइन किया गया है। कृपया ध्यान दें कि अजगर 2 आधिकारिक तौर पर 01-01-2020 के समर्थन से बाहर है। फिर भी, संस्करण-विशिष्ट पायथन सवालों के लिए, [अजगर -२.०] या [अजगर -३.x] टैग जोड़ें। पायथन वेरिएंट (जैसे, ज्योथन, PyPy) या लाइब्रेरी (उदा।, पांडस और न्यूमपी) का उपयोग करते समय, कृपया इसे टैग में शामिल करें।