พร้อมเริ่มต้น Crawl

Web Scraping ด้วย Python

Thomas Laetsch

Data Scientist, NYU

Response คืออะไร

Selector vs Response:

  • Response มีเครื่องมือทั้งหมดที่เรียนรู้จาก Selector:
    • เมธอด xpath และ css ตามด้วยเมธอด extract และ extract_first
  • Response ยัง จดจำ URL ที่โหลด HTML มาด้วย
  • Response ช่วยให้เราย้ายจากเว็บหนึ่งไปยังอีกเว็บหนึ่ง เพื่อ "crawl" เว็บขณะดึงข้อมูล
Web Scraping ด้วย Python

สิ่งที่รู้แล้ว!

  • เมธอด xpath ใช้งานเหมือน Selector
response.xpath( '//div/span[@class="bio"]' )
  • เมธอด css ใช้งานเหมือน Selector
response.css( 'div > span.bio' )
  • การเชื่อมโซ่ใช้งานเหมือน Selector
response.xpath('//div').css('span.bio')
  • การดึงข้อมูลใช้งานเหมือน Selector
response.xpath('//div').css('span.bio').extract()
response.xpath('//div').css('span.bio').extract_first()
Web Scraping ด้วย Python

สิ่งที่ยังไม่รู้

  • response จดจำ URL ไว้ในตัวแปร response url
response.url
>>> 'http://www.DataCamp.com/courses/all'
  • response ให้เรา "follow" ลิงก์ใหม่ได้ด้วยเมธอด follow()
# next_url is the string path of the next url we want to scrape
response.follow( next_url )
  • จะเรียนรู้เพิ่มเติมเกี่ยวกับ follow ในภายหลัง
Web Scraping ด้วย Python

ใน Response

Web Scraping ด้วย Python

Preparing Video For Download...