1

I want to find title, address, price of some items in an online mall. But, sometimes the address is empty and my code is break in my code(below_it's an only selenium part)

num = 1
while 1:
    try:
        title = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div['+str(num)+']/div/div/a/span').text
        datas_title.append(title)

        address = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div['+str(num)+']/div/div/a/div/p[2]').text
        datas_address.append(address)

        price = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div['+str(num)+']/div/div/a/p').text
        datas_price.append(price)
        print('crowling....num = '+str(num))
        
        num=num+1
    except Exception as e:
        print("finish get data...")
        break

print(datas_title)
print(datas_address)
print(datas_price)

what should I do if the address is empty -> just ignore it and find the next items?

0

3 Answers 3

1

Use this so you can skip the entries with missing information:

num = 1
while 1:
    try:
        title = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div['+str(num)+']/div/div/a/span').text
        datas_title.append(title)

        address = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div['+str(num)+']/div/div/a/div/p[2]').text
        datas_address.append(address)

        price = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div['+str(num)+']/div/div/a/p').text
        datas_price.append(price)
        print('crowling....num = '+str(num))
        
        num=num+1
    except:
        print("an error was encountered")
        continue

print(datas_title)
print(datas_address)
print(datas_price)
Sign up to request clarification or add additional context in comments.

Comments

0
address = browser.find_element_by_xpath('//*[@id="root"]/div[1]/section/article/div/div['+str(num)+']/div/div/a/div/p[2]').text
if not address: 
    address = "None"
else: 
    address = address[0].text
datas_title.append(address)

You could use find_elements to check if it's empty and then proceed to do it with either value. You can than encapsulate this into a function pass it the xpath and the data_title array and your code should be repeatable.

Comments

0

I think you need to first check if the web element returned isn't none. And then proceed with fetching text.

You could write a function for it, and catch that exception in it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.