I am trying to get the URLs of all the posts on the page so I can open them in another tab, can someone please help thx!
def opening(self):
bot = self.bot
bot.maximize_window()
bot.get('https://www.instagram.com/explore/tags/'+hashtag+'/')
time.sleep(2)
def posts(self):
bot = self.bot
hrefs = bot.find_element_by_tag_name('a')
pic_hrefs = [elem.get_attribute('href') for elem in hrefs]
pic_hrefs = [href for href in pic_hrefs if hashtag in href]
time.sleep(1)
print(hashtag +' pictures: ' + len(pic_hrefs))
I am a beginner so please don't make fun of me and thank you for everyone that helps!
This line:
hrefs = bot.find_element_by_tag_name('a')
returns a selenium web element. It is not a list, or any other iterable element. In other words, you won't be able to iterate through it with your for loop . Print it and you'll see what it is. Use this line instead, which will return a list. Note that I added an s .
hrefs = bot.find_elements_by_tag_name('a')
Also, did you make a typo in href
? Shouldn't it be hrefs
? The last instance in there:
pic_hrefs = [href for href in pic_hrefs if hashtag in href]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.