dimanche 3 janvier 2021

Python: Web Scraping with input from user

from bs4 import BeautifulSoup
from urllib.request import urlopen as uReq
import requests

url = 'https://en.wikisource.org/wiki/Main_Page'
r = requests.get(url)

Soup = BeautifulSoup(r.text, "html5lib")
List = Soup.find("div",class_="enws-mainpage-widget-content", id="enws-mainpage-newtexts-content").find_all('a')
ebooks=[]
i=0
for ebook in List:
    x=ebook.get('title')
    for ch in x:
        if(ch==":"):
            x=""
    if x!="":
        ebooks.append(x)
        i=i+1
        
print("Please select a book: ")
inputnumber=0
while inputnumber<len(ebooks):
    print(inputnumber+1, " - ", ebooks[inputnumber])
    inputnumber=inputnumber+1
input=int(input())
selectedbook = Soup.find("href", title=ebooks[input-1])
print(selectedbook)

I want to get the href of whichever was selected by user but as output I get: None

Can someone please tell me where I am doing wrong




Aucun commentaire:

Enregistrer un commentaire