【发布时间】:2021-06-10 12:50:55
【问题描述】:
如何让这个脚本抓取列表的 URL (URLS=[]) 并同时检查所有 URL?
for i in range(len(URLs)): 我尝试使用 for range 循环,但它只是一个接一个地测试,我希望它同时运行多个 URL。
PrevVersion = ""
FirstRun = True
url=""
while True:
# download the page
response = requests.get(url, headers=headers)
# parse the downloaded homepage
soup = BeautifulSoup(response.text, "lxml")
# remove all scripts and styles
for script in soup(["script", "style"]):
script.extract()
soup = soup.get_text()
# compare the page text to the previous version
if PrevVersion != soup:
# on the first run - just memorize the page
if FirstRun == True:
PrevVersion = soup
FirstRun = False
print ("Start Monitoring "+url+ ""+ str(datetime.now()))
else:
print ("Changes detected at: "+ str(datetime.now()))
OldPage = PrevVersion.splitlines()
NewPage = soup.splitlines()
# compare versions and highlight changes using difflib
d = difflib.Differ()
diff = d.compare(OldPage, NewPage)
out_text = "\n".join([ll.rstrip() for ll in '\n'.join(diff).splitlines() if ll.strip()])
print (out_text)
OldPage = NewPage
#print ('\n'.join(diff))
PrevVersion = soup
else:
print( "No Changes "+ str(datetime.now()))
time.sleep(10)
continue
此脚本有效,但只有一个 url
【问题讨论】:
标签: python loops python-requests monitoring