回答編集履歴

1

edit

2018/01/09 11:51

投稿

mkgrei
mkgrei

スコア8560

test CHANGED
@@ -11,3 +11,141 @@
11
11
  v = products[a]
12
12
 
13
13
  ```
14
+
15
+
16
+
17
+ ---
18
+
19
+
20
+
21
+ Selenium.webdriverにバグがあってJANコードがあってもたまに取得にミスるみたいですね。
22
+
23
+
24
+
25
+ ```python
26
+
27
+ #!/usr/bin/env python
28
+
29
+ # -*- coding: utf-8 -*-
30
+
31
+ from selenium import webdriver
32
+
33
+ from selenium.common.exceptions import NoSuchElementException
34
+
35
+ from selenium.webdriver.common.keys import Keys
36
+
37
+ import argparse
38
+
39
+
40
+
41
+ DELAY_SLEEP = 1
42
+
43
+ JANJAN_URL = "https://antlion.xsrv.jp/"
44
+
45
+
46
+
47
+ def get_input(fmimic=True):
48
+
49
+ if fmimic:
50
+
51
+ class MimicArgs:
52
+
53
+ def __init__(self):
54
+
55
+ self.jan = [4976219091275, 4873116953, 4873116950000]
56
+
57
+ args = MimicArgs()
58
+
59
+ else:
60
+
61
+ parser = argparse.ArgumentParser(description='Get asin from JANJAN_URL')
62
+
63
+ parser.add_argument('--jan', nargs='+')
64
+
65
+ args = parser.parse_args()
66
+
67
+ return args
68
+
69
+
70
+
71
+ def main():
72
+
73
+ args = get_input()
74
+
75
+
76
+
77
+ driver = webdriver.Chrome()
78
+
79
+
80
+
81
+ products = {}
82
+
83
+ for jan in args.jan:
84
+
85
+ try:
86
+
87
+ driver.get(JANJAN_URL)
88
+
89
+ jan_input_box = driver.find_element_by_xpath('//*[@id="content"]/div[1]/div/form/input[2]')
90
+
91
+ jan_input_box.send_keys(jan)
92
+
93
+ jan_input_button = driver.find_element_by_xpath('//*[@id="content"]/div[1]/div/form/input[3]')
94
+
95
+ jan_input_button.click()
96
+
97
+
98
+
99
+ content = driver.find_elements_by_xpath('//*[@id="content"]/div[2]/div[5]/ul/li')
100
+
101
+ if len(content) == 0:
102
+
103
+ raise NoSuchElementException
104
+
105
+ asin = None
106
+
107
+ for e in content:
108
+
109
+ desc = e.get_attribute('textContent')
110
+
111
+ if desc.startswith("ASIN"):
112
+
113
+ asin = desc.split()[-1]
114
+
115
+ if asin is None:
116
+
117
+ print('ASIN not found')
118
+
119
+ title = driver.find_elements_by_xpath('//*[@id="content"]/div[2]/div[3]/h3/a')[0].text
120
+
121
+ except NoSuchElementException:
122
+
123
+ print("not available")
124
+
125
+ print(jan)
126
+
127
+ continue
128
+
129
+ products[title] = {}
130
+
131
+ v = products[title]
132
+
133
+ v['ASIN'] = asin
134
+
135
+ v['JAN'] = jan
136
+
137
+
138
+
139
+ for k, v in products.items():
140
+
141
+ print(k, v)
142
+
143
+ driver.close()
144
+
145
+
146
+
147
+ if __name__ == "__main__":
148
+
149
+ main()
150
+
151
+ ```