Wizard Book


Programming Python, Web, Android

Python: Web Scrapping dengan BeautifulSoup

Web Scrapping, mengambil informasi halaman web dengan BeautifulSoup

import requests 
from bs4 import BeautifulSoup 

URL = "http://www.kontan.co.id"
r = requests.get(URL) 

soup = BeautifulSoup(r.content, 'html5lib') 
#print(soup.prettify()) 

for core in soup.findAll('div', attrs = {'class':'list-berita'}):

    for row in core.findAll('li'):
        news_url = ''
        news_title = ''

        try:
            news_url=row.a['href']
            try:
                #print(row.findAll('div')[1])#.h1.a.text)
                news_title=row.find('div', attrs = {'class':'sp-hl linkto-black'}).h1.a.text 
            except:
                news_title=""
        except:
            news_title=""
            
    if news_url != "" and news_title != "":
        print(news_url + "--->" + news_title)
        

email : contohprogram.com@gmail.com
WA : +6289671400363