logo

Čitanje odabranog sadržaja web stranice pomoću Python Web Scraping

Preduvjet: Preuzimanje datoteka u Pythonu Web scraping uz BeautifulSoup Svi znamo da je Python vrlo jednostavan programski jezik, ali ono što ga čini cool je veliki broj biblioteka otvorenog koda napisanih za njega. Zahtjevi su jedna od najčešće korištenih biblioteka. Omogućuje nam otvaranje bilo kojeg HTTP/HTTPS web-mjesta i omogućuje nam da radimo sve što inače radimo na webu, a također može spremati sesije, tj. kolačić. Kao što svi znamo da je web stranica samo dio HTML koda koji web poslužitelj šalje našem pregledniku koji se zatim pretvara u prekrasnu stranicu. Sada nam je potreban mehanizam da se dočepamo HTML izvornog koda, tj. pronalaženje određenih oznaka s paketom pod nazivom BeautifulSoup. Montaža:
pip3 install requests 
pip3 install beautifulsoup4 

Uzimamo primjer čitajući stranicu s vijestima Hindustan Times

Kodeks se može podijeliti u tri dijela.
  • Zahtjev za web stranicu
  • Pregledavanje oznaka
  • Ispišite odgovarajući sadržaj
Koraci:
    Traženje web stranice:Prvo vidimo desni klik na tekst vijesti da vidimo izvorni kod Čitanje odabranog sadržaja web stranice pomoću Python Web Scrapinga' title= Provjera oznaka:Moramo shvatiti u kojem se dijelu izvornog koda nalazi dio vijesti koji želimo izbaciti. To je pod uli.e neuređeni popis 'searchNews' koji sadrži dio vijesti. Čitanje odabranog sadržaja web stranice pomoću Python Web Scraping' title= Napomena Tekst vijesti prisutan je u tekstualnom dijelu oznake sidra. Pažljivo promatranje daje nam ideju da su sve vijesti u li popis oznakama neuređene oznake. Čitanje odabranog sadržaja web stranice pomoću Python Web Scraping' title= Ispišite odgovarajući sadržaj: 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() 

    Izlaz

    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 

Reference



Napravi kviz