1

I'm trying to get random wikipedia articles with the wikipedia API and then get all their href links which I did by:

elems = driver.find_elements_by_xpath("//a[@href]")

Then I made:

for elem in elems:
    elements = []

    elements.append(elem.get_attribute("href"))

In order to only get the href links from the received list with the previously executed command. I only got a single link which is:

['https://www.mediawiki.org/']

I tried to do:

for elem in elems:
    elements = []
    elements.extend(elem.get_attribute("href"))

Which resulted in this output:

['h', 't', 't', 'p', 's', ':', '/', '/', 'w', 'w', 'w', '.', 'm', 'e', 'd', 'i', 'a', 'w', 'i', 'k', 'i', '.', 'o', 'r', 'g', '/']

Clearly put: I want to take all href links from a random wikipedia article then put them in a single list called elements which I will later use for a for loop.

Thanks in advance.

Rest of my code if it is needed:

import wikipedia
import selenium
from selenium import webdriver

#This will get a random wikipedia article
random_article = wikipedia.random(1)
#Get the articles URL
url = wikipedia.page(random_article).url

driver = webdriver.Firefox()
driver.get(url)

elems = driver.find_elements_by_xpath("//a[@href]")
for elem in elems:
    elements = []
    elements.extend(elem.get_attribute("href"))

print(elements)
1
  • Thx for accepting my ans as the best ans! Can u also upvote my ans? Thanks! Commented Nov 7, 2020 at 7:43

1 Answer 1

1

You have to initialise the elements list outside the for loop, so this is how for loop should look like:

elements = []

for elem in elems:
    elements.append(elem.get_attribute("href"))

Full code:

import wikipedia
import selenium
from selenium import webdriver

#This will get a random wikipedia article
random_article = wikipedia.random(1)
#Get the articles URL
url = wikipedia.page(random_article).url

driver = webdriver.Firefox()
driver.get(url)

elems = driver.find_elements_by_xpath("//a[@href]")
elements = []
for elem in elems:
    elements.append(elem.get_attribute("href"))

print(elements)
Sign up to request clarification or add additional context in comments.

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.