您好,我在管理變量內(nèi)的 html 代碼時(shí)遇到問題。我想制作一個(gè)程序,在網(wǎng)站上登錄帳戶,然后檢查特定位置的某些內(nèi)容是否已更改,如果是,則向我發(fā)送電子郵件或某種通知。import requestsfrom bs4 import BeautifulSoup as bstab = []payload = { 'plates': 'login', 'vin': 'password'}with requests.Session() as s: p = s.post('url', data=payload) print (p.text)soup = bs(p.text,'html.parser')body = soup.find_all("li",{"class" : "state"},"span")a = body[2]print (a)p.text是整個(gè)html代碼身體看起來像這樣[<li class="state first"><p><span>Przyj?to wniosek, trwa post?powanie administracyjne</span></p><img class="arrow" src="//cdn.info-car.pl/webfiles/1590137977316/img/document/selected_status.png"/></li>, <li class="state active"><p><span>Dokument zosta? zamówiony</span></p><img class="arrow" src="//cdn.info-car.pl/webfiles/1590137977316/img/document/selected_status_active.png"/></li>, <li class="state"><p><span>Dokument do odbioru w urz?dzie</span></p><img class="arrow" src="//cdn.info-car.pl/webfiles/1590137977316/img/document/selected_status.png"/></li>, <li class="state last"><p><span>Dokument wydany</span></p><img class="arrow" src="//cdn.info-car.pl/webfiles/1590137977316/img/document/selected_status.png"/></li>]主體[2]看起來像這樣:<li class="state"><p><span>Dokument do odbioru w urz?dzie</span></p><img class="arrow" src="//cdn.info-car.pl/webfiles/1590137977316/img/document/selected_status.png"/></li>src="//cdn.info-car.pl/webfiles/1590137977316/img/document/selected_status.png"/>現(xiàn)在我的計(jì)劃是僅從body[2] 中選擇然后將其與包含相同文本的另一個(gè)變量進(jìn)行比較(我從網(wǎng)絡(luò)上抓取的 body[2] 中的變量希望有一天會(huì)改變)如果這兩個(gè)變量不相同,那么做一些事情(我想我會(huì)處理消息部分)我只是不知道如何選擇我想要的部分。像管理字符串一樣管理它很困難,因?yàn)樗羞@些奇怪的字符(<、>、“、'、/)等。我想我希望我的最終解決方案是這樣的if The_part_I_want == The old html code: passelse: send me a notification
1 回答

尚方寶劍之說
TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊
搜索img帶有class=arrowin的元素body[2]并獲取其src屬性。
the_old_url = "//cdn.info-car.pl/webfiles/1590137977316/img/document/selected_status.png"
img = body[2].find('img', class_='arrow')
if img and img['src'] == the_old_url:
pass
else:
send_notification()
添加回答
舉報(bào)
0/150
提交
取消