Reputation: 379
I am trying to scrape like/views from instagram. however getting error below:
IndexError: list index out of range :--- when i try viewcth[0].get_attribute('innerHTML')
AttributeError: 'list' object has no attribute 'get_attribute' :--- when i try viewct = viewcth.get_attribute('innerHTML')
code:
viewcth = bdy.find_elements_by_xpath(".//*[@class='eo2As ']//*[@class='EDfFK ygqzn']//*[@class='Nm9Fw']")
if (len(viewcth) != 0):
viewct = viewcth[0].get_attribute('innerText')
else:
viewcth = bdy.find_elements_by_xpath(".//*[@class='eo2As ']//*[@class='HbPOm _9Ytll']//*[@class='vcOH2']")
# viewct = viewcth.get_attribute('innerHTML')
viewct = viewcth[0].get_attribute('innerHTML')
pagedict['viewcount'] = viewct
print("Viewct is " + viewct)
Upvotes: 2
Views: 664
Reputation: 29362
See viewcth
is a list in Python.
and you have stated this :
AttributeError: 'list' object has no attribute 'get_attribute' :--- when i try viewct = viewcth.get_attribute('innerHTML')
so when you can not use get_attribute()
on list. But in your code I see, you have not used viewcth.get_attribute('innerHTML')
instead it is
viewcth[0].get_attribute('innerHTML')
which is right I think.
I have used a counter, to count total comment.
sample code :
counter = 0
viewcth = driver.find_elements_by_xpath(".//*[@class='eo2As ']//*[@class='EDfFK ygqzn']//*[@class='Nm9Fw']")
if (len(viewcth) != 0):
abc = viewcth[0].get_attribute('innerHTML')
counter = counter + 1
else:
viewcth2 = driver.find_elements_by_xpath(".//*[@class='eo2As ']//*[@class='HbPOm _9Ytll']//*[@class='vcOH2']")
cde = viewcth2[0].get_attribute('innerHTML')
counter = counter + 1
print("total view is ", counter)
Update 1 :
You can try the below xpath :
to get only numbers :
//textarea/ancestor::section/preceding-sibling::section[1]/descendant::span
code :
wait = WebDriverWait(driver, 10)
print(wait.until(EC.element_to_be_clickable((By.XPATH, "//textarea/ancestor::section/preceding-sibling::section[1]/descendant::span"))).text)
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Upvotes: 1
Reputation: 33361
You are using a wrong locator.
This is why viewcth
is and empty list.
find_elements_by_xpath(".//*[@class='eo2As ']//*[@class='EDfFK ygqzn']//*[@class='Nm9Fw']")
finds no matching elements and returns empty list.
So when you trying to get a first element from that list by viewcth[0]
you are getting
IndexError: list index out of range
.
If you trying to perform viewct = viewcth.get_attribute('innerHTML')
this gives you
AttributeError: 'list' object has no attribute 'get_attribute'
since viewcth
is a list. Empty, but still a list.
So you can not apply .get_attribute('innerHTML')
method on a list, it is not a web element.
If you want to get amount of like try this:
For images
likes = bdy.find_element_by_xpath(".//a[@class='zV_Nj']/span").text
For videos:
likes = bdy.find_element_by_xpath(".//div[@class='Nm9Fw']/a").text
Or
likes = bdy.find_element_by_xpath(".//div[@class='HbPOm _9Ytll']/span").text
Upvotes: 1