简体   繁体   中英

I am unable to figure out why my code throws a TypeError

I am trying to run a while loop which basically detect changes in a website. But when I ever I run the code I get the following error:

Traceback (most recent call last):
  File "/Users/me/Desktop/parse.py", line 22, in <module>
    main()
  File "/Users/me/Desktop/parse.py", line 19, in main
    old_data = soup.copy()
TypeError: 'NoneType' object is not callable
[Finished in 0.461s]

Here's my code:

import requests
from bs4 import BeautifulSoup
from functools import partial


def parse(text):
    soup = BeautifulSoup(text, 'lxml')
    return soup

def main():
    URL = 'https://www.apple.com/macbook-pro-16/'
    session = requests.Session()
    old_data = None
    while True:
        r = session.get(URL)
        new_data = parse(r.text)
        if new_data != old_data:
            print('new_data')
            old_data = new_data.copy()

if __name__ == "__main__":
    main()

BeautifulSoup doc says:

You can use copy.copy() to create a copy of any Tag or NavigableString

Could you try

import copy  # should be at top of the file
old_data = copy.copy(new_data)

Explanation

From BS code:

def __getattr__(self, tag):
        #print "Getattr %s.%s" % (self.__class__, tag)
        if len(tag) > 3 and tag.endswith('Tag'):
            ... # deprecated code
        # We special case contents to avoid recursion.
        elif not tag.startswith("__") and not tag == "contents":
            return self.find(tag)

What happens?

new_data.copy is the same as new_data.find('copy') and return None (there is no <copy> tag).

Than you do this:

old_data = new_data.find('copy')()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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