Slide 1

Slide 1 text

Build Your Own Web Scraper - Dale Ma @eguitarz 12年12月8日星期六

Slide 2

Slide 2 text

@eguitarz It’s fun to do something small and easy. 12年12月8日星期六

Slide 3

Slide 3 text

@eguitarz I always want to build a robot to serve me. 12年12月8日星期六

Slide 4

Slide 4 text

@eguitarz Since making a robot is too difficult, so I choose to make a web bot. 12年12月8日星期六

Slide 5

Slide 5 text

@eguitarz Today I’m talking about how do I build my own web scraper in ruby. 12年12月8日星期六

Slide 6

Slide 6 text

@eguitarz Web scrapers have many uses. For example... 12年12月8日星期六

Slide 7

Slide 7 text

@eguitarz Up time survey, image collecting, automate web snapshots and more... 12年12月8日星期六

Slide 8

Slide 8 text

@eguitarz Usually, there are many scrapers (threads) fired at the same time. 12年12月8日星期六

Slide 9

Slide 9 text

@eguitarz So, first things first, I have to control the threads. 12年12月8日星期六

Slide 10

Slide 10 text

@eguitarz I decide to write #threadpool to do this such thing. 12年12月8日星期六

Slide 11

Slide 11 text

@eguitarz You can find that at https:// github.com/eguitarz/threadpool 12年12月8日星期六

Slide 12

Slide 12 text

@eguitarz Threadpool decides the life of each thread. 12年12月8日星期六

Slide 13

Slide 13 text

@eguitarz Now, let’s go for the main dish. 12年12月8日星期六

Slide 14

Slide 14 text

@eguitarz Web scrappers should be able to `grab page` and `parse html tags`. 12年12月8日星期六

Slide 15

Slide 15 text

@eguitarz #Nokogiri is good at those things. 12年12月8日星期六

Slide 16

Slide 16 text

@eguitarz I use “Hash” to save parsed links. 12年12月8日星期六

Slide 17

Slide 17 text

@eguitarz There’s a problem, links stored in hash by threads. But hash in ruby is not thread-safe... 12年12月8日星期六

Slide 18

Slide 18 text

@eguitarz #hamster helps me with this. 12年12月8日星期六

Slide 19

Slide 19 text

@eguitarz I use `Depth-Limited Search` algorithm for my scrapper. 3 2 1 1 12年12月8日星期六

Slide 20

Slide 20 text

@eguitarz What if the page needs javascript to render? 12年12月8日星期六

Slide 21

Slide 21 text

@eguitarz There’s a easy way... use browser to render the html with javascript. 12年12月8日星期六

Slide 22

Slide 22 text

@eguitarz How? 12年12月8日星期六

Slide 23

Slide 23 text

@eguitarz #Waltir or #Selenium 12年12月8日星期六

Slide 24

Slide 24 text

Gonna show my little toy... 12年12月8日星期六

Slide 25

Slide 25 text

@eguitarz My scraper is on github at https:// github.com/eguitarz/macaron 12年12月8日星期六

Slide 26

Slide 26 text

@eguitarz The demo is simple, `you` can enhance or create new one. 12年12月8日星期六

Slide 27

Slide 27 text

@eguitarz Wikipedia scraper, Facebook scraper... could be interesting! 12年12月8日星期六

Slide 28

Slide 28 text

THANKS! 12年12月8日星期六