I have this code that uses BeautifulSoup to gather some data from a website
import requests
from bs4 import BeautifulSoup
url = "http://hearthstone.gamepedia.com/Patches"
page = requests.get(url)
soup = BeautifulSoup(page.content,"html.parser")
variable = soup.find('div',{"id":"mw-content-text"})
variable = variable.find_all('ul')[2]
variable = variable.find('li')
variable = variable.find_all('a')[1]
print(variable.text)
Output should be:
Patch 7.0.0.15590
in this order, I am able to locate the exact a tags that I want.
How could I make this a single line code in order to simplify it?
Variable = harsoup.find('div',{"id":"mw-content-text"}).find_all('ul')[2].find('li').find_all('a')[1]
I wanted to achieve something like this but it doest seem to work the same way.
out:
use
re
to fileter the a tag you want.There are five filters that can be used in the
find()
orfind_all()
: