0% menganggap dokumen ini bermanfaat (0 suara)
128 tayangan25 halaman

Panduan Web Scraping dengan Python

Data Computational Journalism Conference Indonesia 2022 memberikan informasi tentang konferensi tersebut yang diselenggarakan pada tanggal 29 Juli 2022. Konferensi ini membahas tentang penggunaan bahasa pemrograman Python untuk keperluan web scraping serta mendemonstrasikan contoh kode Python untuk mengambil data dari berbagai sumber seperti website dan media sosial.

Diunggah oleh

esjipi
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PDF, TXT atau baca online di Scribd
0% menganggap dokumen ini bermanfaat (0 suara)
128 tayangan25 halaman

Panduan Web Scraping dengan Python

Data Computational Journalism Conference Indonesia 2022 memberikan informasi tentang konferensi tersebut yang diselenggarakan pada tanggal 29 Juli 2022. Konferensi ini membahas tentang penggunaan bahasa pemrograman Python untuk keperluan web scraping serta mendemonstrasikan contoh kode Python untuk mengambil data dari berbagai sumber seperti website dan media sosial.

Diunggah oleh

esjipi
Hak Cipta
© All Rights Reserved
Kami menangani hak cipta konten dengan serius. Jika Anda merasa konten ini milik Anda, ajukan klaim di sini.
Format Tersedia
Unduh sebagai PDF, TXT atau baca online di Scribd

Data Computational Journalism

Conference Indonesia 2022


29 Juli 2022

FOR

LAKSONO HARI WIWOHO


laksonohw@[Link]
08118484495
Basic Python untuk

PENT!NG
SUPER web scraping saja
beloem tjoekoep!
• Mustahil tanpa paham HTML
karena ini dasar membangun web
• Python itu powerfull, belajarnya
enggak mudah. Pengamanan web
juga makin ketat. Yakin cuma
untuk scraping kecil-kecilan?
Jangan tanggung, bestie!
DCJI 2022
DON’T
QUIT!!
Mulailah belajar otodidak dari
[Link]
HTML CSS Python

DCJI 2022
KENAPA PYTHON ?
• Python merupakan bahasa Apa bedanya dengan R?
pemrograman multifungsi
(web apps, data science, • R difokuskan untuk olah
mesin otomatisasi, hingga data/statistik
bikin game). • Integrasi terbatas dengan
• Bisa integrasi dengan aplikasi/platform lain
aplikasi/platform lain • Butuh memori besar
• Banyak forum & library

DCJI 2022
Demo dikit boleh, yah

Tarik data channel Youtube & Tiktok


From this… …to this!
From this… …to this!
From this… …to this!

JEO [Link]
From this… …to this!
Animasi menggunakan Tableau
Scraping

DCJI 2022
Server berisi:
• html, css, js, php, asp, dll…
• jpg, gif, png, mp4, dll… Sisi server
• csv, json, pdf, database, dll…

[Link]
3
Server mencari file [Link]
& merespons
dengan HTTP
[Link]
response
[Link]
[Link]
Server kirim
default file
Oops, no file HTML
2 Jika file tak ada di 5
Browser server, akan muncul Layout halaman
kirim HTTP pesan eror ditampilkan di browser
1 request
Ketik URL
di browser
4
Browser membaca
kode HTML. Jika ada
Sisi klien gambar atau file
tambahan, browser
minta lagi ke server
DCJI 2022
Target
• Buka [Link]
– Cermati dulu strukturnya
– Telusuri satu per satu halamannya
– Tentukan bagian mana yang mau ditarik
• Kembali ke halaman utama, klik kanan browser, pilih
Inspect
• Bandingkan isinya dengan View Source

Di sinilah pentingnya memahami HTML sebelum scraping

DCJI 2022
Bagian HL Kenali cangkang atau kontainer dari
berisi 1 artikel masing-masing komponen yang
menjadi target scraping
Bagian HL 2 Pelajari isi dari masing-masing
berisi 2 artikel kontainer tadi dan tentukan bagian
mana yang perlu diambil datanya

Bagian indeks
berisi 5 artikel

DCJI 2022
Mari mulai Python
Instalasi Python
Ada beberapa alternatif instalasi:
1. Unduh dan instal dari [Link]
2. Cari dan instal Python basic dari Microsoft Store
3. Unduh dan instal aplikasi Anaconda. Jalankan Python
melalui apps Jupyter

Kebutuhan library
1. Requests *  HTTP library
2. JSON *  konversi & kelola data format json
3. BeautifulSoup (bs4)  web scraping
4. Pandas  olah data tabel, ekspor data ke Excel/csv
* biasanya sudah tersedia dalam paket Python
Jika sudah instal aplikasi Python:
1. Buka jendela Command Prompt di PC
2. Ketik python lalu Enter
Panduan selanjutnya
menggunakan
Jika belum instal Python: online console ini
1. Jalankan web browser dan bukalah
[Link]
2. Muncul boks, klik New notebook di bagian bawah
3. Ubah nama file di kiri atas ([Link])
4. Klik baris kosong di bawah + Code, mari mulai
menuliskan perintah berikutnya
Basic Python
Menambahkan library
import requests
from bs4 import BeautifulSoup
import pandas

Jika belum menginstal library, Anda harus menginstalnya lebih


dulu di Command Prompt dengan perintah berikut
pip install requests
pip install bs4
pip install pandas

DCJI 2022
Basic Python
Sintax
>>>print("Halo, Ferguso") Ini baris perintah
Halo, Ferguso Ini hasil eksekusi perintah

Indentation
>>>if 5 > 2: >>>if 5 > 2:
... print("Benar, 5 > 2") ...print("Benar, 5 > 2")
Benar, 5 > 2 IndentationError: expected
an indented block

DCJI 2022
Basic Python
Variabel
>>>x = 5 # x bertipe integer (numerik)
>>>y = "Namaku Pitung" # y bertipe string (teks)
>>>nama, usia, kelas = "Pitung", 12, "8C"

>>>X = str(3) # 3 menjadi '3' (string)


>>>Y = int('03') # 3 menjadi 3 (numerik)
>>>Z = float(3) # 3 menjadi 3.0 (desimal)

Nama variabel bersifat case sensitive, tidak boleh pakai spasi,


tidak bisa diawali dengan angka
DCJI 2022
Basic Python
Tipe data
x = "Apa kabar?" # string
y = 20 # integer
z = 20.5 # float
a = ["apel", "brokoli", "caisim"] # list
B = ("apel", "brokoli", "caisim") # tuple
c = range(6) # range
d = {"nama" : "Pitung", "usia" : 36} # dict
_ = {"Andini", "Bambang", "Cahyono"} # set
p = True # boolean (True/False)
k = b"Halo Pitung" # bytes
D = None # NoneType

DCJI 2022
Basic Python
Tipe data
x = "Apa kabar?"
print(x) # bagaimana hasilnya?
Apa kabar?

x = {"nama" : "Pitung", "usia" : 36}


print(x) # bagaimana hasilnya?
{'nama': 'Pitung', 'usia': 36}

DCJI 2022
Basic Python
Komparasi Jenis Operator:
Komparasi
== sama dengan
x = 1
>= lebih besar sama dengan
if x == 1:
print("x = 1") > lebih besar dari
elif x == 2: != tidak sama dengan
print("x = 2")
else: Logika
print("entahlah") and dan
# bagaimana hasilnya? or atau
x = 1 not kebalikan

dan lain-lain
DCJI 2022
Basic Python
Pengulangan (looping)
for x in range(5):
print(x) #bagaimana hasilnya?
0 Urutan indeks SELALU diawali
dengan 0 (nol), bukan 1
1
2
3
4

DCJI 2022
ke sungai cari kepiting,
mari mulai scraping
Praktik Scraping

Bukalah ini dan praktikkan*


[Link]

* Untuk praktik, salinlah kode pada halaman di atas ke Notebook


milik Anda sendiri. Jalankan kode di Notebook Anda, bukan pada
tautan di atas

DCJI 2022

Common questions

Didukung oleh AI

Knowing different data types in Python assists in effective programming as it helps in appropriately storing, managing, and manipulating data. Understanding data types like strings, integers, lists, dictionaries, and more is crucial to define variables correctly, optimize code functionality, and enhance clarity and efficiency .

Increasing web security challenges web scraping by implementing measures like CAPTCHAs, secured login protocols, and dynamic webpage rendering which disrupt automated data extraction processes. These challenges require scrapers to adapt by developing more sophisticated tools or using alternative approaches like API access where available .

After setting up the environment for Python web scraping, perform the following steps: identify and examine the target webpage's structure, use HTML inspection tools like 'Inspect' to locate elements, write scripts using libraries like Requests and BeautifulSoup to send requests and parse the HTML content, and extract, manipulate, and store the desired data using data handling libraries like Pandas .

Python might be preferred by beginners due to its multifaceted nature, enabling usage in data science, web development, and more, along with its straightforward syntax which eases the learning curve. Additionally, Python's extensive libraries and community support make it a strong candidate for web scraping and data science .

Python is known for its versatility, as it supports web apps, data science, automation, and even game development, and can easily integrate with other applications and platforms. In contrast, R is more focused on data manipulation and statistical analysis, but it has limited application and platform integration capabilities and generally requires more memory for operations .

The key libraries required for web scraping with Python are: Requests, which handles HTTP requests; JSON for converting and managing data in JSON format; BeautifulSoup (bs4) for parsing HTML and XML documents; and Pandas for data manipulation and exporting data to formats like Excel or CSV .

Understanding HTML is essential before engaging in web scraping because HTML forms the foundation of web structure. Familiarity with HTML allows one to identify the components of a webpage that need to be extracted and understand the layout and organization of the content, which is crucial for effective data retrieval .

Indentation in Python is significant because it denotes blocks of code. Incorrect indentation can cause errors, such as IndentationError, which can lead to misinterpretation of code structure and logic, affecting the execution flow .

Precautions when using web scraping include understanding a website's terms of service regarding data extraction to avoid legal issues, implementing respectful scraping techniques to prevent overloading servers, and ensuring data privacy and security by not extracting sensitive or personal information without permission .

The HTTP request-response cycle is pivotal in web scraping as it involves the browser sending an HTTP request to the server, which then responds with the HTML content of a webpage. This cycle allows scrapers to access the necessary HTML data, parse it, and extract relevant information. Understanding this cycle is crucial in simulating browser requests and handling responses effectively .

Anda mungkin juga menyukai