Answer the question
In order to leave comments, you need to log in
Python BeautifulSoup NameError: name 'src' is not?
Hello everyone, I am writing a parser for wildberries
Here is the whole code:
from bs4 import BeautifulSoup
import requests
from requests.api import head
import io
import random
import numpy
def get_html():
'''Получение html кода'''
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
}
url = 'https://www.wildberries.ru/catalog/muzhchinam/odezhda/vodolazki'
html = requests.get(url,headers=headers)
global src
src = html.text
def get_pages():
"""получение количества страниц"""
soup = BeautifulSoup(src , 'lxml')
try:
good_count = soup.find('h1').find_next('span').get_text(strip=True).replace("\xa0", '').split()[0]
pages = int(good_count) // 100 + 1
except:
pages = 1
print(f'''Количество страниц:{pages}''')
def get_price():
'''получение цены товара'''
soup = BeautifulSoup(src , 'lxml')
h1 = soup.find_all(class_="lower-price")
print(h1)
def main ():
get_price()
get_html()
get_pages()
if __name__ == '__main__':
main()
Answer the question
In order to leave comments, you need to log in
1. the global variable itself is declared a level higher
2. but, you don’t need to use global, especially for a beginner
3. you must explicitly return the result from the function, just as explicitly (and not through global) pass this result to another function
PS the order of the function call, hints at a problem with logic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question