-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalueget.py
More file actions
135 lines (117 loc) · 6.18 KB
/
valueget.py
File metadata and controls
135 lines (117 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# encoding: utf-8
from getdata import *
class ShopValue:
def __init__(self):
self.pageTool = PageData()
self.hisTool = PageData()
self.hisTool.makeCompile()
self.webList = ['http://www.jd.com/', 'https://www.tmall.com/', 'https://www.amazon.cn/',
'http://www.suning.com/']
self.xpathList = [
[
"//input[@clstag='h|keycount|2015|03a']",
"//ul[@class='J_valueList v-fixed']",
"//ul[@class='J_valueList v-fixed']/li/a", # brand -text
"//div[@class='gl-i-wrap']",
"//div[@class='gl-i-wrap']/div[@class='p-name p-name-type-2']/a", # Name -title
"//div[@class='gl-i-wrap']/div[@class='p-commit']", # Hot -text
"//div[@class='gl-i-wrap']/div[@class='p-price']/strong", # Fee -text
"//div[@class='gl-i-wrap']/div[@class='p-img']/a", # Url -href
"//div[@class='gl-i-wrap']/div[@class='p-img']/a/img", # Page -src
"//button[@clstag='h|keycount|2015|03c']" # 需要改成click
],
[
"//div[@class='s-combobox-input-wrap']/input",
"//ul[@class='av-collapse row-2']",
"//ul[@class='av-collapse row-2']/li/a", # -text
"//div[@class='product ']",
#天猫一号 Name 属性
"//div[@class='product ']/div/p[@class='productTitle']/a", # -title
"//div[@class='product ']/div/p[@class='productStatus']/span", # -text
"//div[@class='product ']/div/p[@class='productPrice']/em", # -text
"//div[@class='product ']/div/div[@class='productImg-wrap']/a", # -href
"//div[@class='product ']/div/div[@class='productImg-wrap']/a/img" # -src
#天猫二号 Name 属性
"//div[@class='product ']/div/div[@class='productTitle productTitle-spu']", # -text
],
[
"//input[@id='twotabsearchtextbox']",
"//div[@class='refinements']/ul[@id='ref_125596071']",
# "//ul[@id='ref_125596071']/li", #-text
# "//ul[@id='s-results-list-atf']",
"//a[@class='a-link-normal s-access-detail-page a-text-normal']", # -title
# "//div[@class='a-row a-spacing-none']/span/a[@class='a-size-small a-link-normal a-text-normal']", #-text
"//span[@class='a-size-base a-color-price s-price a-text-bold']", # -text
"//div[@class='a-section a-spacing-none a-inline-block s-position-relative']/a", # -href
"//div[@class='a-section a-spacing-none a-inline-block s-position-relative']/a/img" # src
],
[
"//input[@id='searchKeywords']",
"//div[@class='brand-item brand-con']",
"//div[@class='brand-item brand-con']/div/a", # -text
"//ul[@class='clearfix']",
"//p[@class='sell-point']/a", # -title
"//p[@class='com-cnt']", # -text
"//p[@class='prive-tag']/em", # -text
"//div[@class='img-block']/a", # -href
"//div[@class='img-block']/a/img", # -src
]
]
self.shopName = [u"京东", u"天猫", u"亚马逊", u"苏宁易购"]
def getValue(self,Key):
resultData={}
for shopIter in range(1):
MidData = {}
self.pageTool.driver.get(self.webList[shopIter])
self.pageTool.driver.find_element_by_xpath(self.xpathList[shopIter][0]).send_keys(Key)
if shopIter == 0:
self.pageTool.driver.find_element_by_xpath(self.xpathList[shopIter][9]).click()
else:
self.pageTool.driver.find_element_by_xpath(self.xpathList[shopIter][0]).send_keys(Keys.RETURN)
resultData[self.shopName[shopIter]] = ""
#得到第一个商品的信息
try:
wait(self.pageTool.driver, 3).until(lambda theDriver: theDriver.find_element_by_xpath(self.xpathList[shopIter][3]).is_displayed())
except Exception:
print 11111
resultData[self.shopName[shopIter]] = ""
continue
#天猫屎一样的布局 fuck
if shopIter != 1:
Name = self.pageTool.driver.find_element_by_xpath(
self.xpathList[shopIter][4]).text # text
else:
try:
Name = self.pageTool.driver.find_elements_by_xpath(self.xpathList[shopIter][4])[0].text # text
except:
try:
Name = self.pageTool.driver.find_elements_by_xpath(self.xpathList[shopIter][9])[0].text
except:
resultData[self.shopName[shopIter]] = ""
Hot = self.pageTool.driver.find_elements_by_xpath(
self.xpathList[shopIter][5])[0].text # text
Fee = self.pageTool.driver.find_elements_by_xpath(
self.xpathList[shopIter][6])[0].text # float(text)
Url = self.pageTool.driver.find_elements_by_xpath(
self.xpathList[shopIter][7])[0].get_attribute("href") # href
Page = self.pageTool.driver.find_elements_by_xpath(
self.xpathList[shopIter][8])[0].get_attribute("src") # src
# solve the history fee data problem
try:
if shopIter == 3:
hispat = self.hisTool.patterns['suning'].search(Url)
Url = hispat.group(1) + hispat.group(2)
except AttributeError:
pass
hisData = self.hisTool.getHisData(Url)
hisData = hisData if hisData != '' else ''
MidData["goodsName"] = Name
MidData["goodsFee"] = Fee
MidData["goodsHis"] = hisData
MidData["goodsPage"] = Page
MidData["goodsHot"] = Hot
MidData["goodsFrom"] = self.shopName[shopIter]
resultData[self.shopName[shopIter]]=MidData
result=json.dumps(resultData)
print result