Upon asking the user for input it returns a empty list, I've tried looking at other answers with similar issues but none of them solved mine.
url = "https://en.wikipedia.org/wiki/Main_Page"
resp = requests.get(url).text
page = BeautifulSoup(resp, "html.parser")
def Get_Links():
inp = input("Enter data you wish to find: ")
datavar = page.find_all(inp, 'a', href=True)
print(datavar)
Result:
[]
CodePudding user response:
You need to pass as string
argument to the find_all
function for the text (text inside the anchor tag) search.
def Get_Links():
inp = input("Enter data you wish to find: ")
datavar = page.find_all('a', href=True, string=inp)
return datavar
Execution:
In [1]: Get_Links()
Enter data you wish to find: 2012
Out[1]: [<a href="/wiki/2012" title="2012">2012</a>]