1

I have build a list which contains href from website and i wanna randomly select one of this link, how can i do that?


from bs4 import BeautifulSoup
import urllib
import requests
import re
import random

url = "https://www.formula1.com/en/latest.html"
articles = []
respone = urllib.request.urlopen(url)
soup = BeautifulSoup(respone,'lxml')

def getItems():
       for a in soup.findAll('a',attrs={'href': re.compile("/en/latest/article.")}):
           articles = a['href']
           x = random.choice(articles)
           print(x)

That code work, but selecting only random index from all of the objects

  • For those of use unfamiliar with beautiful soup, What, exactly, is `a['href']`? can you `print(repr(a['href'])`? I suspect it is a *string* not a list. – juanpa.arrivillaga Aug 06 '21 at 00:06
  • Probably, `articles` is a `str`. Try `random.choice(articles.split())` – htzfun Aug 06 '21 at 00:08
  • Does this answer your question? [How can I randomly select an item from a list?](https://stackoverflow.com/questions/306400/how-can-i-randomly-select-an-item-from-a-list) – pigeonburger Aug 06 '21 at 00:12

1 Answers1

2

You're very close to the answer. You just need to do this:

from bs4 import BeautifulSoup
import urllib
import requests
import re
import random

url = "https://www.formula1.com/en/latest.html"
articles = []
respone = urllib.request.urlopen(url)
soup = BeautifulSoup(respone,'lxml')

def getItems():
       for a in soup.findAll('a',attrs={'href': re.compile("/en/latest/article.")}):
           articles.append(a['href'])
       x = random.choice(articles)
       print(x)

getItems()

The changes are:

  • We add each article to the articles array.
  • The random choice is now done after the loop, rather than inside the loop.
Robson
  • 2,008
  • 2
  • 7
  • 26
  • If this will be used for anything where someone might try to guess, use `secrets.choice(articles)` rather than `random.choice(articles)`? – Jiří Baum Aug 06 '21 at 00:13