質問編集履歴

3

質問に関係ない部分を修正しました

2017/07/28 08:27

投稿

jackojacko_
jackojacko_

スコア17

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ###前提・実現したいこと
2
2
 
3
- カーセンサーの中古車検索API(https://webservice.recruit.co.jp/carsensor/reference.html)を用いてスクレイピングをしようとしています。
3
+ カーセンサーnetの中古車検索API(https://webservice.recruit.co.jp/carsensor/reference.html)を用いてスクレイピングをしようとしています。
4
4
 
5
5
  BeautifulSoupを使っているのですが、<body>内の<name>を出力したいのに、以下のコードではbodyより手前にある<brand>内の<name>が出力されてしまいます。
6
6
 
@@ -10,7 +10,7 @@
10
10
 
11
11
  ###発生している問題・エラーメッセージ
12
12
 
13
- カーセンサーのAPIを使うと、たとえば車種「プリウス」で検索すると以下のようなXMLが出力されます。
13
+ カーセンサーnetのAPIを使うと、たとえば車種「プリウス」で検索すると以下のようなXMLが出力されます。
14
14
 
15
15
  以下の「ミニバン」を取得したいのに、「トヨタ」が取得されてしまいます。
16
16
 
@@ -68,42 +68,6 @@
68
68
 
69
69
  </body>
70
70
 
71
- <odd>1km</odd>
72
-
73
- <year>2017</year>
74
-
75
- <shop>
76
-
77
- <name>カーズカフェ カーベル姫路東</name>
78
-
79
- <pref>
80
-
81
- <code>28</code>
82
-
83
- <name>兵庫県</name>
84
-
85
- </pref>
86
-
87
- <lat>34.796492</lat>
88
-
89
- <lng>134.721361</lng>
90
-
91
- <datum>world</datum>
92
-
93
- </shop>
94
-
95
- <color>各色選べます(オプション色は32,400円高)</color>
96
-
97
- <maintenance_comment>
98
-
99
- 9型フルセグ地デジ対応高詳細ナビ(DVDビデオ再生可)CD録音機能&Bluetooth接続&USB接続&ニンテンドーDS接続&バックカメラ&アンテナ分離型ETC&フロアマットを取り付け済でお渡しです!新車をご注文後メーカーよりお取り寄せいたしますので、現行最新モデルでのご準備となります。ボディーカラーお選びいただけます(オプション色は32,400円高)メーカーオプション等の追加もできます!
100
-
101
- </maintenance_comment>
102
-
103
- <maintenance_fee/>
104
-
105
- <photo>
106
-
107
71
  (以下略)
108
72
 
109
73
  </usedcar>
@@ -116,7 +80,7 @@
116
80
 
117
81
  ###該当のソースコード
118
82
 
119
- ```ここに言語を入力
83
+ ```Python
120
84
 
121
85
  import lxml.html
122
86
 
@@ -126,17 +90,13 @@
126
90
 
127
91
  from bs4 import BeautifulSoup
128
92
 
129
- import pandas as pd
130
93
 
131
-
132
-
133
- carcode=[]
134
94
 
135
95
  bodytype=[]
136
96
 
137
97
 
138
98
 
139
- url='http://webservice.recruit.co.jp/carsensor/usedcar/v1/?key=4c3c5fca5e53632a&model='
99
+ url='http://webservice.recruit.co.jp/carsensor/usedcar/v1/?key=(APIキー)&model='
140
100
 
141
101
 
142
102
 
@@ -144,39 +104,25 @@
144
104
 
145
105
 
146
106
 
147
- for i in range(1,10):
107
+ driver.get(url + "プリウス")
148
108
 
149
- driver.get(url + "プリウス")
109
+ data = driver.page_source.encode('utf-8')
150
110
 
151
- data = driver.page_source.encode('utf-8')
152
-
153
- soup = BeautifulSoup(data, "lxml")
111
+ soup = BeautifulSoup(data, "lxml")
154
112
 
155
113
 
156
114
 
157
- if soup.find("body").find("name"):
115
+ if soup.find("body").find("name"):
158
116
 
159
- carcode.append(i)
117
+ body = soup.find("body")
160
118
 
161
- body = soup.find("body")
119
+ bodytype.append(body.find("name").string)
162
120
 
163
- bodytype.append(body.find("name").string)
121
+ else:
164
122
 
165
- else:
166
-
167
- carcode.append(i)
168
-
169
- bodytype.append("NA")
123
+ bodytype.append("NA")
170
124
 
171
125
 
172
-
173
- df = pd.DataFrame({"carcode":carcode,"bodytype":bodytype})
174
-
175
-
176
-
177
- df.to_csv("result.csv", index=False, encoding='utf-8')
178
-
179
-
180
126
 
181
127
  driver.quit()
182
128
 

2

2017/07/28 08:27

投稿

jackojacko_
jackojacko_

スコア17

test CHANGED
@@ -1 +1 @@
1
- BeautifulSoupでのスクレイピングがうまくできない
1
+ BeautifulSoupで親要素指定を無視して子要素取得されてしま
test CHANGED
File without changes

1

コード中の不要な部分を消しました

2017/07/27 04:38

投稿

jackojacko_
jackojacko_

スコア17

test CHANGED
File without changes
test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
  <code>TO</code>
36
36
 
37
- **<name>トヨタ</name>**
37
+ <name>トヨタ</name>
38
38
 
39
39
  </brand>
40
40
 
@@ -64,7 +64,7 @@
64
64
 
65
65
  <code>M</code>
66
66
 
67
- **<name>ミニバン</name>**
67
+ <name>ミニバン</name>
68
68
 
69
69
  </body>
70
70
 
@@ -130,8 +130,6 @@
130
130
 
131
131
 
132
132
 
133
- dat=pd.read_csv('carcode.csv',encoding="UTF-8")
134
-
135
133
  carcode=[]
136
134
 
137
135
  bodytype=[]
@@ -139,16 +137,6 @@
139
137
 
140
138
 
141
139
  url='http://webservice.recruit.co.jp/carsensor/usedcar/v1/?key=4c3c5fca5e53632a&model='
142
-
143
- params=dict(
144
-
145
- key="4c3c5fca5e53632a",
146
-
147
- model="プリウス"
148
-
149
- #model=dat.iloc[1,1]
150
-
151
- )
152
140
 
153
141
 
154
142
 
@@ -158,7 +146,7 @@
158
146
 
159
147
  for i in range(1,10):
160
148
 
161
- driver.get(url + dat.iloc[i,1])
149
+ driver.get(url + "プリウス")
162
150
 
163
151
  data = driver.page_source.encode('utf-8')
164
152
 
@@ -168,7 +156,7 @@
168
156
 
169
157
  if soup.find("body").find("name"):
170
158
 
171
- carcode.append(dat.iloc[i,0])
159
+ carcode.append(i)
172
160
 
173
161
  body = soup.find("body")
174
162
 
@@ -176,7 +164,7 @@
176
164
 
177
165
  else:
178
166
 
179
- carcode.append(dat.iloc[i,0])
167
+ carcode.append(i)
180
168
 
181
169
  bodytype.append("NA")
182
170