Привет, у меня какая-то ошибка при добавлении данных в csv - PullRequest
0 голосов
/ 08 мая 2020

Я не могу добавить данные в csv У меня есть ошибка (Имя телефона не определено)

i = 1
while i <10:
        try:
            print(driver.current_url)
        except Exception:
            print('Internet Error Detected')
        try:
            title = driver.find_element_by_xpath('//*[@id="titletextonly"]').text
            print(title)
        except Exception:
            print('No Title Given')
        try:
            price = driver.find_element_by_xpath('/html/body/section/section/h2/span/span[2]').text
            print(price)
        except Exception:
            print('No Price Given')
        try:
            phone = driver.find_element_by_xpath('//*[@id="postingbody"]/h2[1]/big').text
            print(phone)
        except Exception:
            print('No Mobile number avalible')
        try:
            loc = driver.find_element_by_xpath('/html/body/section/section/section/div[1]/div/div[2]').text
            print(loc)
        except Exception:
            print('No Location Data Avalible')
        try:
            img = page_soup.find('img')
            print(img)
            # immg = print(img.get('src'))
        except Exception:
            print('No img Found')  
        records.append(((driver.current_url, title, price, img, phone, loc)))
        i+=1

Говорится только телефон, а вот c не определен, почему я не знаю, можете ли вы помочь мне

1 Ответ

0 голосов
/ 08 мая 2020

Может быть гораздо более элегантный способ сделать это с меньшим количеством операторов try / except (лично я ненавижу их слишком много). Также похоже, что значения не будут присвоены, если есть ошибка в любом из этих блоков. Я добавил оператор <variable> = None за исключением каждой попытки:

i = 1
# It looks like this will just do the same thing to the same page 9 times
# and will put 9 identical rows in your csv unless there is more code
while i < 10:
    try:
        print(driver.current_url)
    except Exception:
        print('Internet Error Detected')
        break # <---------- exit the loop
    try:
        title = driver.find_element_by_xpath('//*[@id="titletextonly"]').text
        print(title)
    except Exception:
        title = None # <---- return none if error, did this for each varable
        print('No Title Given')
    try:
        price = driver.find_element_by_xpath('/html/body/section/section/h2/span/span[2]').text
        print(price)
    except Exception:
        price = None # <------------
        print('No Price Given')
    try:
        phone = driver.find_element_by_xpath('//*[@id="postingbody"]/h2[1]/big').text
        print(phone)
    except Exception:
        phone = None # <------------
        print('No Mobile number avalible')
    try:
        loc = driver.find_element_by_xpath('/html/body/section/section/section/div[1]/div/div[2]').text
        print(loc)
    except Exception:
        loc = None # <------------
        print('No Location Data Avalible')
    try:
        img = page_soup.find('img')
        print(img)
        # immg = print(img.get('src'))
    except Exception:
        img = None # <------------
        print('No img Found')
    records.append(((driver.current_url, title, price, img, phone, loc)))
    i += 1
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...