【问题标题】:Checking multiple URLs for content change at the same time同时检查多个 URL 的内容更改
【发布时间】: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


    【解决方案1】:

    如果您想检查所有三个,您可能需要多线程。 你应该定义一个函数,然后用不同的线程调用它。

    【讨论】:

    • 好主意,我试过了,但我无法让他们不在我的 url 列表中的某一点检查同一行,你将如何让他们分配要读取的 url。跨度>
    猜你喜欢
    • 1970-01-01
    • 2015-02-17
    • 2014-03-06
    • 1970-01-01
    • 2013-02-22
    • 2012-10-10
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多