スクレイピングは HTML のソースコードと比べながら、自分のほしい情報をどうやったらとれるかセレクターを考えるのがポイントです。普段 CSS や Javascript を書いてる人は得意かもしれません。
サンプルコード
python
1 import csv
2 import urllib
3
4 from bs4 import BeautifulSoup
5
6 url = 'https://dubai.dubizzle.com/motors/search/?page=1' # 取得先URL
7 html = urllib . request . urlopen ( url ) . read ( )
8 soup = BeautifulSoup ( html , 'html.parser' )
9
10 data = [ ]
11 for item_elems in soup . select ( 'div.list-item-wrapper' ) :
12 # 年代、走行距離
13 li_elems = item_elems . select ( 'ul.features > li' )
14 year = li_elems [ 0 ] . text . replace ( 'Year: ' , '' ) # 年代
15 km = li_elems [ 1 ] . text . replace ( 'Kilometers: ' , '' ) # 走行距離
16 # メーカー、種類
17 breads = item_elems . select ( 'p.breadcrumbs' ) [ 0 ] . text
18 breads = [ s . replace ( '\u202a' , '' ) . strip ( ) for s in breads . split ( '>' ) ] # \u202a は消す
19 maker = breads [ 1 ]
20 car_type = breads [ 2 ]
21 # 値段
22 price_elem = item_elems . select ( 'div.price' ) [ 0 ]
23 price = price_elem . text . replace ( ',' , '' ) . replace ( 'AED' , '' ) . strip ( ) # , と AED は消す
24 # リンク
25 a_elem = item_elems . select ( 'h3 a' ) [ 0 ]
26 car_url = urllib . parse . urljoin ( url , a_elem . get ( 'href' ) )
27 title = a_elem . text . strip ( )
28
29 data . append ( {
30 'year' : year ,
31 'km' : km ,
32 'maker' : maker ,
33 'type' : car_type ,
34 'price' : price ,
35 'title' : title ,
36 'url' : car_url
37 } )
38
39 from pprint import pprint
40 pprint ( data )
output
1 [{'km': '31,000',
2 'maker': 'Bentley',
3 'price': '399000',
4 'title': 'Bentley Continental Flying Spur W12 2014 Full...',
5 'type': 'Continental Flying Spur',
6 'url': 'https://dubai.dubizzle.com/motors/used-cars/bentley/continental-flying-spur/2017/12/30/bentley-continental-flying-spur-w12-2014-f-2/?back=L21vdG9ycy9zZWFyY2gvP3BhZ2U9MQ%3D%3D&pos=1',
7 'year': '2014'},
8 {'km': '27,600',
9 'maker': 'Bentley',
10 'price': '360000',
11 'title': 'BENTLEY CONTINENTAL GT 2014 MODEL V8 GCC',
12 'type': 'Continental GT',
13 'url': 'https://dubai.dubizzle.com/motors/used-cars/bentley/continental-gt/2018/7/1/bentley-continental-gt-2014-model-v8-gcc-2/?back=L21vdG9ycy9zZWFyY2gvP3BhZ2U9MQ%3D%3D&pos=2',
14 'year': '2014'},
15 {'km': '40,000',
16 'maker': 'Audi',
17 'price': '130000',
18 'title': '2016 Audi TT Coupe - Monsoon Grey',
19 'type': 'TT',
20 'url': 'https://dubai.dubizzle.com/motors/used-cars/audi/tt/2018/10/2/2016-audi-tt-coupe-monsoon-grey-2/?back=L21vdG9ycy9zZWFyY2gvP3BhZ2U9MQ%3D%3D&pos=3',
21 'year': '2016'},
22 {'km': '30000',
23 'maker': 'Ferrari',
24 'price': '595000',
25 'title': 'FERRARI F12 BERLINETTA,2013 MODEL,GOOD CONDIT...',
26 'type': 'F12',
27 'url': 'https://dubai.dubizzle.com/motors/used-cars/ferrari/f12/2018/9/1/ferrari-f12-berlinetta-2013-model-good-con-2/?back=L21vdG9ycy9zZWFyY2gvP3BhZ2U9MQ%3D%3D&pos=4',
28 'year': '2013'},
29 {'km': '100000',
30 'maker': 'Porsche',
31 'price': '26500',
32 'title': 'PORSCHE CAYENNE S,2008 MODEL,EXCELLENT CONDIT...',
33 'type': 'Cayenne',
34 'url': 'https://dubai.dubizzle.com/motors/used-cars/porsche/cayenne/2018/7/12/porsche-cayenne-s-2008-model-excellent-con-2/?back=L21vdG9ycy9zZWFyY2gvP3BhZ2U9MQ%3D%3D&pos=5',
35 'year': '2008'},
36 {'km': 'Usage: Never Used',
37 'maker': 'Sailboats',
38 'price': '1719950',
39 'title': 'DUFOUR 56 EXCLUSIVE',
40 'type': 'Sailing Yacht',
41 'url': 'https://dubai.dubizzle.com/motors/boats/sailboats/sailing-yacht/2018/10/11/dufour-56-exclusive-2/?back=L21vdG9ycy9zZWFyY2gvP3BhZ2U9MQ%3D%3D&pos=6',
42 'year': 'Age: Brand New'},
43 {'km': '180,000',
44 'maker': 'Honda',
45 'price': '28000',
46 'title': 'HONDA ACCORD',
47 'type': 'Accord',
48 'url': 'https://dubai.dubizzle.com/motors/used-cars/honda/accord/2018/10/2/honda-accord-2/?back=L21vdG9ycy9zZWFyY2gvP3BhZ2U9MQ%3D%3D&pos=7',
49 'year': '2014'},
50 {'km': '202,000',
51 'maker': 'Honda',
52 'price': '17000',
53 'title': 'Honda Accord',
54 'type': 'Accord',
55 'url': 'https://dubai.dubizzle.com/motors/used-cars/honda/accord/2018/10/11/honda-accord-2/?back=L21vdG9ycy9zZWFyY2gvP3BhZ2U9MQ%3D%3D&pos=8',
56 'year': '2008'},
57 {'km': '210,570',
58 'maker': 'Honda',
59 'price': '10000',
60 'title': 'Honda civic Lxi 2006 gcc Agency maintained f...',
61 'type': 'Civic',
62 'url': 'https://dubai.dubizzle.com/motors/used-cars/honda/civic/2018/10/11/honda-civic-lxi-2006-gcc-for-sale-2/?back=L21vdG9ycy9zZWFyY2gvP3BhZ2U9MQ%3D%3D&pos=9',
63 'year': '2006'},
64 {'km': '140000',
65 'maker': 'Ford',
66 'price': '27000',
67 'title': 'Family Used, Gulf Regional Specs 2012 Ford Ed...',
68 'type': 'Edge',
69 'url': 'https://dubai.dubizzle.com/motors/used-cars/ford/edge/2018/10/9/2012-ford-edge-awd-2/?back=L21vdG9ycy9zZWFyY2gvP3BhZ2U9MQ%3D%3D&pos=10',
70 'year': '2012'}]
複数ページをスクレイピングする方法について
こちらの回答 を参考にしてください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/11 06:30