ดึงผลหวยจากเว็บ Sanook ด้วย Code 11 บรรทัด

ดึงผลหวยจากเว็บ Sanook ด้วย Code 11 บรรทัด

ดึงผลหวยจากเว็บ Sanook.com ด้วย BeautifulSoup และ requests แบบไม่ใช้ selenium

  • ติดตั้ง BeautifulSoup ก่อนด้วยคำสั่งต่อไปนี้
pip install beautifulsoup4
  • ทำการ Import Package requests และ BeautifulSoup
import requests
from bs4 import BeautifulSoup
  • ประกาศตัวแปรเพื่อเก็บ URL หน้าเช็คผลหวยของเว็บ Sanook.com
url = "https://news.sanook.com/lotto/"
  • เขียนคำสั่งเพื่อดึงข้อมูลจาก URL ที่เราตั้งค่าไว้
response = requests.request("GET", url)
  • แปลงข้อมูลที่ได้จาก response เป็น html praser ด้วย BeautifulSoup
soup = BeautifulSoup(response.text)
  • ทำการคัดลอก css selector ของเว็บ sanook ดังรูป
  • เขียนโค๊ตเพื่อดึงข้อมูลจากจุดที่เราคัดลอกไว้ด้านบน
# รางวัลที่ 1
first_prize = soup.select_one("#lotto-highlight-result > span:nth-child(1) > strong").text
  • ลองแสดงค่าที่ได้ออกมาดู
print({
    "first_prize":first_prize
})

จากนั้นทำซ้ำไปเรื่อยๆกัน รางวัลที่เราต้องการ จนครบทั้งหมด

Code เต็มทั้งหมดครับ หรือทดสอบได้ที่นี้ “ดึงผลหวยจากเว็บ Sanook ด้วย beautifulsoup4 และ requests.ipynb

import requests
from bs4 import BeautifulSoup

url = "https://news.sanook.com/lotto/"

response = requests.request("GET", url)

soup = BeautifulSoup(response.text)

# รางวัลที่ 1
first_prize = soup.select_one("#lotto-highlight-result > span:nth-child(1) > strong").text

# เลขหน้า 3 ตัว
first_3_digits_01 = soup.select_one("#lotto-highlight-result > span:nth-child(2) > b:nth-child(1)").text
first_3_digits_02 = soup.select_one("#lotto-highlight-result > span:nth-child(2) > b:nth-child(2)").text

# เลขท้าย 3 ตัว
last_3_digits_01 = soup.select_one("#lotto-highlight-result > span:nth-child(3) > b:nth-child(1)").text
last_3_digits_02 = soup.select_one("#lotto-highlight-result > span:nth-child(3) > b:nth-child(2)").text

# เลขท้าย 2 ตัว
last_2_digits = soup.select_one("#lotto-highlight-result > span:nth-child(4) > strong").text


print({
    "รางวัลที่ 1":first_prize,
    "เลขหน้า 3 ตัว":[first_3_digits_01,first_3_digits_02],
    "เลขท้าย 3 ตัว":[last_3_digits_01,last_3_digits_02],
    "เลขท้าย 2 ตัว":last_2_digits
})

Read more

สำรวจ Walt Disney Studios Lot โฉมใหม่: ประสบการณ์อันน่าทึ่งที่ Disney's Hollywood Studios

สำรวจ Walt Disney Studios Lot โฉมใหม่: ประสบการณ์อันน่าทึ่งที่ Disney's Hollywood Studios

ค้นพบการเปลี่ยนแปลงครั้งใหญ่ที่ Walt Disney Studios Lot ใน Disney's Hollywood Studios สัมผัสหมวก Sorcerer Mickey ที่ส่องแสง พื้นที่พักผ่อนใหม่ และการทักทายตัวละคร!

By ทีมงาน devdog
บิ๊กแมตช์ลาลีกา: บียาร์เรอัล ปะทะ แอตเลติโก มาดริด ศึกชี้ชะตาอันดับ 3 ในปี 2026

บิ๊กแมตช์ลาลีกา: บียาร์เรอัล ปะทะ แอตเลติโก มาดริด ศึกชี้ชะตาอันดับ 3 ในปี 2026

วิเคราะห์เจาะลึกแมตช์สำคัญ บียาร์เรอัล ปะทะ แอตเลติโก มาดริด นัดสุดท้ายลาลีกา 2026 สถิติ ฟอร์มการเล่น และคาดการณ์ผลบอล คู่ชิงอันดับ 3!

By ทีมงาน devdog