简体   繁体   English

同时检查多个 URL 的内容更改

[英]Checking multiple URLs for content change at the same time

How would I make this script grab URLs of a list (URLS=[]) and check all URLs at the same time?我如何让这个脚本抓取列表的 URL (URLS=[]) 并同时检查所有 URL?

for i in range(len(URLs)): i tried using for range loop but it's just test one after another, I want it to run multiple URLs at the same time. 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

This script works but only one url此脚本有效,但只有一个 url

If you want to check all three you might need multithreading.如果要检查所有三个,则可能需要多线程。 You should define a function and then call it with different threads.您应该定义一个 function 然后用不同的线程调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM