I have a problem when web scraping python

I have to scraping data from ebay.com , my scraping item’s is : ‘title of car’, ‘make’, ‘model’, ‘transmission’ and ‘price’, I have scraped all of this item’s but when write code of ‘transmission’, show me this error:

Traceback (most recent call last):
  File "C:/Users/Oogway/PycharmProjects/web_scraping1/test.py", line 27, in <module>
    trans_of_car = title_trans.text
AttributeError: 'NoneType' object has no attribute 'text'

And it does not scrape the ‘transmission’ item!. *tip : Scrape a few items from ‘transmission’, then show the error and after showing the error scrape a tow item of ‘transmission’.

code:

import requests
from bs4 import BeautifulSoup
import re

url = 'https://www.ebay.com/b/Cars-Trucks/6001?_fsrp=0&_sacat=6001&LH_BIN=1&LH_ItemCondition=3000%7C1000%7C2500&rt=nc&_stpos=95125&Model%2520Year=2020%7C2019%7C2018%7C2017%7C2016%7C2015'
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')

ebay_cars = soup.find_all('li', class_='s-item')
for car_info in ebay_cars:

    title_div = car_info.find('div', class_='s-item__wrapper clearfix')
    title_sub_div = title_div.find('div', class_='s-item__info clearfix')
    title_p = title_sub_div.find('span', class_='s-item__price')
    title_tag = title_sub_div.find('a', class_='s-item__link')
    title_maker = title_sub_div.find('span', class_='s-item__dynamic s- item__dynamicAttributes1')
    title_model = title_sub_div.find('span', class_='s-item__dynamic s-item__dynamicAttributes2')
    title_trans = title_sub_div.find('span', class_='s-item__dynamic s-item__dynamicAttributes3')

    name_of_car = title_tag.text
    price_of_car = title_p.text
    maker_of_car = title_maker.text
    model_of_car = title_model.text
    trans_of_car = title_trans.text

Answer

This is because, this element is not present in the website for this specific entry, which means it is None. Just put the statement in a try , except clause, which is causing this error and this will help..

In this case…

try:
  trans_of_car = title_trans.text
except:
  trans_of_car = ''

Leave a Reply

Your email address will not be published. Required fields are marked *