mardi 6 novembre 2018

Adding BeautifulSoup find element in a list

I have the following code :

import requests
from bs4 import BeautifulSoup

page = requests.get("https://www.seminuevos.com/usados/-/autos/-/volkswagen")
soup = BeautifulSoup(page.content, "html.parser")

kkt = soup.find_all("div", class_="card hoverable") # mazga
for tag in kkt:
    print(tag.find('a', href=True)['href'])

With this code i am getting the links that i want(printed only), but i cannot append them to a list. If i use :

list = []
for tag in kkt:
    item = tag.find('a', href=True)['href']
    list.append(item)

I get this error :

Traceback (most recent call last):
  File "<input>", line 3, in <module>
TypeError: 'NoneType' object is not subscriptable

How can i append the result of : for tag in kkt: item = tag.find('a', href=True)['href'] to a list, line by line ? I`ve also tried :

list = [tag.find('a', href=True)['href'] for tag in kkt]

Not working.




Aucun commentaire:

Enregistrer un commentaire