Q
Q
Qvint012020-02-02 21:55:18
Python
Qvint01, 2020-02-02 21:55:18

How to choose a random string?

hello i have a code

spoiler
import requests
from json import loads
from bs4 import BeautifulSoup
import random

s = requests.session()
s.headers.update({'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'})
 
r = s.get('https://www.google.ru/search?q=кот&tbm=isch')
 
soup = BeautifulSoup(r.text, "html.parser")
 
for i in range(0,10):
  for text in soup.findAll(attrs={'class': 'rg_meta notranslate'}):
      text = loads(text.text)
      print(text['ou'])
it gives the following:

5e3719dd2add6634249213.png
And many more lines... (that is, the usual url parser of a photo from Google, in this case = 'cat')
From all this, you need to select only one url and place it in the

Ps variable, the code was taken from the Internet

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2020-02-02
@Qvint01

from random import choice

soup = BeautifulSoup(r.text, "html.parser")
elems = soup.findAll(attrs={'class': 'rg_meta notranslate'})
pic = loads(choice(elems).text)['ou']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question