logo

Lettura del contenuto della pagina Web selezionato utilizzando Python Web Scraping

Prerequisito: Scaricare file in Python Web Scraping con BeautifulSoup Sappiamo tutti che Python è un linguaggio di programmazione molto semplice, ma ciò che lo rende interessante è il gran numero di librerie open source scritte per esso. Requests è una delle librerie più utilizzate. Ci consente di aprire qualsiasi sito Web HTTP/HTTPS e di eseguire qualsiasi tipo di operazione che eseguiamo normalmente sul Web e può anche salvare sessioni, ad esempio cookie. Come tutti sappiamo, una pagina web è solo un pezzo di codice HTML che viene inviato dal server Web al nostro browser che a sua volta si converte nella bellissima pagina. Ora abbiamo bisogno di un meccanismo per ottenere il codice sorgente HTML, ad esempio trovare alcuni tag particolari con un pacchetto chiamato BeautifulSoup. Installazione:
pip3 install requests 
pip3 install beautifulsoup4 

Prendiamo un esempio leggendo un sito di notizie Tempi dell'Hindustan

Il codice può essere diviso in tre parti.
  • Richiesta di una pagina web
  • Ispezione dei tag
  • Stampa il contenuto appropriato
Passaggi:
    Richiesta di una pagina web:Per prima cosa vediamo fare clic con il tasto destro sul testo della notizia per vedere il codice sorgente Lettura del contenuto della pagina Web selezionato utilizzando Python Web Scraping' title= Ispezione dei tag:Dobbiamo capire in quale corpo del codice sorgente contiene la sezione delle notizie che vogliamo eliminare. È l'elenco non ordinato sotto uli.e "searchNews" che contiene la sezione delle notizie. Lettura del contenuto della pagina Web selezionato utilizzando Python Web Scraping' title= Nota Il testo delle notizie è presente nella parte di testo del tag di ancoraggio. Un'osservazione ravvicinata ci dà l'idea che tutte le notizie si trovano nella lista dei tag del tag non ordinato. Lettura del contenuto della pagina Web selezionato utilizzando Python Web Scraping' title= Stampa il contenuto appropriato: The content is printed with the help of code given below. Python
    import requests from bs4 import BeautifulSoup def news(): # the target we want to open  url='http://www.hindustantimes.com/top-news' #open with GET method resp=requests.get(url) #http_respone 200 means OK status if resp.status_code==200: print('Successfully opened the web page') print('The news are as follow :-n') # we need a parserPython built-in HTML parser is enough . soup=BeautifulSoup(resp.text'html.parser') # l is the list which contains all the text i.e news  l=soup.find('ul'{'class':'searchNews'}) #now we want to print only the text part of the anchor. #find all the elements of a i.e anchor for i in l.findAll('a'): print(i.text) else: print('Error') news() 

    Produzione

    Successfully opened the web page The news are as follow :- Govt extends toll tax suspension use of old notes for utility bills extended till Nov 14 Modi Abe seal historic civil nuclear pact: What it means for India Rahul queues up at bank says it is to show solidarity with common man IS kills over 60 in Mosul victims dressed in orange and marked 'traitors' Rock On 2 review: Farhan Akhtar Arjun Rampal's band hasn't lost its magic Rumours of shortage in salt supply spark panic among consumers in UP Worrying truth: India ranks first in pneumonia diarrhoea deaths among kids To hell with romance here's why being single is the coolest way to be India vs England: Cheteshwar Pujara Murali Vijay make merry with tons in Rajkot Akshay-Bhumi SRK-Alia Ajay-Parineeti: Age difference doesn't matter anymore Currency ban: Only one-third have bank access; NE backward regions worst hit Nepal's central bank halts transactions with Rs 500 Rs 1000 Indian notes Political upheaval in Punjab after SC tells it to share Sutlej water Let's not kid ourselves with Trump what we have seen is what we will get Want to colour your hair? Try rose gold the hottest hair trend this winter 

Riferimenti



Crea quiz