Cannot select item names. Where is the inaccuracy in the code?

#!/usr/bin/env python import urllib from bs4 import BeautifulSoup def get_html(url): response = urllib.urlopen(url) return response.read() def parse(html): soup = BeautifulSoup(html) table = soup.find('form', class_='box-katalog') projects = [] for row in table.find_all('h2'): cols = row.find_all('h2') projects.append({ 'title': cols[0].a.alt }) for projects in projects: print(projects) def main(): print(get_html('http://www.elix-c.ru/')) if __name__ == '__main__': main() 
  • And where you call parse (), there is no call in the code. - betonimig

1 answer 1

In the markup, the form tag has no class box-katalog , it has a div . The class_ argument is the one that the tag has. Read more here: Searching by CSS class

 #!/usr/bin/env python import urllib from bs4 import BeautifulSoup def get_html(url): response = urllib.urlopen(url) return response.read() def parse(html): products = [] soup = BeautifulSoup(html) table = soup.find_all('div', class_='box-katalog') for row in table: for col in row.find_all('h2'): products.append({ 'title': col.text }) for product in products: print(product['title']) def main(): html = get_html('http://www.elix-c.ru/') parse(html) if __name__ == '__main__': main() 
  • displays not all positions - titov_andrei
  • @titov_andrei, corrected, but I think you thought it out yourself - betonimig