回答編集履歴

3

解決策の追加

2017/09/12 03:42

投稿

jackojacko_
jackojacko_

スコア17

test CHANGED
@@ -1,3 +1,61 @@
1
+ ##追記
2
+
3
+ ご回答を受けて、<body>の子タグの<body>の子タグの<name>と指定したところ、そちらでもうまくいきました!
4
+
5
+ (パーサーを他に替えてもどうしてもHTMLと認識してしまうこともあるので……)
6
+
7
+
8
+
9
+ ```Python
10
+
11
+ import lxml.html
12
+
13
+ import selenium
14
+
15
+ from selenium import webdriver
16
+
17
+ from bs4 import BeautifulSoup
18
+
19
+
20
+
21
+ bodytype=[]
22
+
23
+
24
+
25
+ url='http://webservice.recruit.co.jp/carsensor/usedcar/v1/?key=(APIキー)&model='
26
+
27
+
28
+
29
+ driver = webdriver.Chrome('C:\selenium\chromedriver')
30
+
31
+
32
+
33
+ driver.get(url + "プリウス")
34
+
35
+ data = driver.page_source.encode('utf-8')
36
+
37
+ soup = BeautifulSoup(data, "lxml")
38
+
39
+
40
+
41
+ if soup.find("name"):
42
+
43
+ bodytype.append(soup.find("body").find("body").find("name").string)
44
+
45
+ else:
46
+
47
+ bodytype.append(None)
48
+
49
+
50
+
51
+ driver.quit()
52
+
53
+ ```
54
+
55
+
56
+
57
+ ##自己解決
58
+
1
59
  根本的な原因はわからないままなのですが、「<body>の子要素の<name>」という風に親要素との関連で定義するのを諦めて、「2つ目の<name>タグ」という条件で指定するようにしたところ、一応本来の目的は達成できました。
2
60
 
3
61
 

2

文法を修正

2017/09/12 03:42

投稿

jackojacko_
jackojacko_

スコア17

test CHANGED
@@ -36,8 +36,6 @@
36
36
 
37
37
  if soup.find("name"):
38
38
 
39
- body = soup.find("body")
40
-
41
39
  bodytype.append(soup.find("name")[1].string)
42
40
 
43
41
  else:

1

文法を修正

2017/07/28 08:35

投稿

jackojacko_
jackojacko_

スコア17

test CHANGED
@@ -34,7 +34,7 @@
34
34
 
35
35
 
36
36
 
37
- if soup.find("name")[1].string:
37
+ if soup.find("name"):
38
38
 
39
39
  body = soup.find("body")
40
40