S
S
sega111sega2021-07-05 21:29:58
Python
sega111sega, 2021-07-05 21:29:58

Why does the error occur: 'NoneType' object has no attribute 'group'?

Good afternoon!

Error intermittently:
Traceback (most recent call last):
File "/home/oksana/Desktop/main/main.py", line 14, in
first_link = re.search(r'(?<=par_picture":"\ \/\\/).*?(?=")', markup).group(0)
AttributeError: 'NoneType' object has no attribute 'group'

res = requests.get(url)
src = res.content
soup = BeautifulSoup(src, 'lxml')
markup = soup.prettify()


first_link = re.search(r'(?<=par_picture":"\\/\\/).*?(?=")', markup).group(0)
print(first_link)

first_number = re.search(r'(?<=480\\/).*?(?=.jpg)', first_link).group(0)
print(first_number)

numbers = re.findall(r'\bdata-image-id="\s*([^"]+)', markup)
print(numbers)


And it does not always appear, but every once in a while. It can happen a few times and then stop. How can I fix it?
PS: perhaps the problem is getting the html page and I don't get it when I call it often? Added the missing part of the code above.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-07-05
@sega111sega

The resource you requested does not always return the expected data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question