logo

Geselecteerde webpagina-inhoud lezen met Python Web Scraping

Voorwaarde: Bestanden downloaden in Python Webscrapen met BeautifulSoup We weten allemaal dat Python een heel gemakkelijke programmeertaal is, maar wat het cool maakt, is het grote aantal open source-bibliotheken dat ervoor is geschreven. Requests is een van de meest gebruikte bibliotheken. Het stelt ons in staat om elke HTTP/HTTPS-website te openen en ons alle dingen te laten doen die we normaal op internet doen, en we kunnen ook sessies opslaan, bijvoorbeeld cookies. Zoals we allemaal weten, is een webpagina slechts een stukje HTML-code dat door de webserver naar onze browser wordt verzonden en die op zijn beurt wordt omgezet in de prachtige pagina. Nu hebben we een mechanisme nodig om de HTML-broncode te bemachtigen, d.w.z. het vinden van een aantal specifieke tags met een pakket genaamd BeautifulSoup. Installatie:
pip3 install requests 
pip3 install beautifulsoup4 

We nemen een voorbeeld door een nieuwssite te lezen Hindoestaanse tijden

De code kan in drie delen worden verdeeld.
  • Een webpagina aanvragen
  • Het inspecteren van de tags
  • Druk de juiste inhoud af
Stappen:
    Een webpagina aanvragen:Eerst zien we dat we met de rechtermuisknop op de nieuwstekst klikken om de broncode te zien Geselecteerde webpagina-inhoud lezen met Python Web Scraping' title= De tags inspecteren:We moeten uitzoeken in welke hoofdtekst van de broncode de nieuwssectie staat die we willen schrappen. Het is de onder uli.e ongeordende lijst 'searchNews' die de nieuwssectie bevat. Geselecteerde webpagina-inhoud lezen met Python Web Scraping' title= Opmerking De nieuwstekst is aanwezig in het tekstgedeelte van de ankertag. Een nauwkeurige observatie geeft ons het idee dat al het nieuws in de lijsttags van de ongeordende tag staat. Geselecteerde webpagina-inhoud lezen met Python Web Scraping' title= Druk de juiste inhoud af: 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() 

    Uitvoer

    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 

Referenties



Quiz maken