Разница между 2 списками - PullRequest
       33

Разница между 2 списками

0 голосов
/ 30 сентября 2018

У меня возникает следующая проблема при выводе элементов в списке, которых нет во втором списке.

Вот код:

def getInitialList(): # Define initial list with the use of requests and BS, will return a set

    getInHtml = requests.get("http://127.0.0.1")
    parseInHtml = BeautifulSoup(getInHtml.content, "html.parser")

    processInHtml = parseInHtml.find_all("div", class_="inner-article")

    firstList = []

    for items in processInHtml:
        firstList.append(items)

    return firstList

def getSecList(): #Define second list with the use of requests and BS, will return a set
    getHtml = requests.get("http://127.0.0.1")
    parseHtml = BeautifulSoup(getHtml.content, "html.parser")

    processHtml = parseHtml.find_all("div", class_="inner-article")

    secList = []

    for items in processHtml:
        secList.append(items)

    return secList

def catch_new_item():

    initList = getInitialList()

    while True:
        if initList == getSecList():
            print("No new items")
        else:
            print("New items found")
            break
    secList = getSecList()
    return set(secList) - set(initList)

Эта последняя функция (catch_new_items ()) должен возвращать то, что находится в secList, а не в initList, но когда я запускаю его, он возвращает пустой набор.

Адрес 127.0.0.1 - это локальный веб-сервер, который я запускаю, чтобы определитьразница между этими 2 пунктами.Все, что я делаю, это редактирую HTML и добавляю еще один элемент.

Дайте мне знать, что вы думаете?

1 Ответ

0 голосов
/ 30 сентября 2018

Я изменил код таким образом, чтобы отладить его:

def getInitialList(): # Define initial list with the use of requests and BS, will return a set

    firstList = ['1', '2', '3']
    return firstList

def getSecList(): #Define second list with the use of requests and BS, will return a set

    secList = ['a', 'b', '3', '1']
    return secList

def catch_new_item():

    initList = getInitialList()
    while True:
        if initList == getSecList():
            print("No new items")
        else:
            print("New items found")
        break
    secList = getSecList()
    return set(secList) - set(initList)

print(catch_new_item())

и он возвращает:

New items found
{'a'}

Так что логика обнаружения элементов хороша.

  1. Вы пытались просто распечатать свои списки из функций getInitialList () и getSecList (), чтобы увидеть, являются ли они пустыми?
  2. Действительно ли списки содержат разные элементы?(если они не пусты, см. стр. 1)
...