logo

Ausgewählte Webseiteninhalte mit Python Web Scraping lesen

Voraussetzung: Herunterladen von Dateien in Python Web Scraping mit BeautifulSoup Wir alle wissen, dass Python eine sehr einfache Programmiersprache ist, aber was sie so cool macht, ist die große Anzahl an Open-Source-Bibliotheken, die dafür geschrieben wurden. Requests ist eine der am häufigsten genutzten Bibliotheken. Es ermöglicht uns, jede HTTP/HTTPS-Website zu öffnen und alle möglichen Dinge zu tun, die wir normalerweise im Web tun, und kann auch Sitzungen speichern, z. B. Cookies. Wie wir alle wissen, ist eine Webseite nur ein Stück HTML-Code, der vom Webserver an unseren Browser gesendet wird, der wiederum in eine schöne Seite umgewandelt wird. Jetzt brauchen wir einen Mechanismus, um an den HTML-Quellcode zu gelangen, also bestimmte Tags mit einem Paket namens BeautifulSoup zu finden. Installation:
pip3 install requests 
pip3 install beautifulsoup4 

Wir nehmen ein Beispiel, indem wir eine Nachrichtenseite lesen Hindustan Times

Der Code kann in drei Teile unterteilt werden.
  • Anforderung einer Webseite
  • Inspektion der Tags
  • Drucken Sie den entsprechenden Inhalt aus
Schritte:
    Eine Webseite anfordern:Zuerst sehen wir, wie wir mit der rechten Maustaste auf den Nachrichtentext klicken, um den Quellcode anzuzeigen Ausgewählte Webseiteninhalte mit Python Web Scraping lesen' title= Überprüfung der Tags:Wir müssen herausfinden, in welchem ​​Teil des Quellcodes sich der Nachrichtenabschnitt befindet, den wir entfernen möchten. Es ist die unter uli.e ungeordnete Liste „searchNews“, die den News-Bereich enthält. Ausgewählte Webseiteninhalte mit Python Web Scraping lesen' title= Hinweis Der Nachrichtentext ist im Anker-Tag-Textteil vorhanden. Eine genaue Beobachtung lässt uns vermuten, dass sich alle Nachrichten in Li-Listen-Tags des ungeordneten Tags befinden. Ausgewählte Webseiteninhalte mit Python Web Scraping lesen' title= Drucken Sie den entsprechenden Inhalt aus: 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() 

    Ausgabe

    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 

Referenzen



Quiz erstellen