-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaobao.py
73 lines (58 loc) · 1.94 KB
/
taobao.py
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
from selenium import webdriver
import threading
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import requests
import os
from selenium.webdriver.chrome.options import Options
headers={
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
}
key_word=input('keyword:')
if not os.path.exists(key_word):
os.mkdir(key_word)
def down_img(root_path,ind,url,headers):
img_path=root_path+'/{}.jpg'.format(ind)
response=requests.get('http:'+url,headers=headers)
with open(img_path,'ab+')as f:
f.write(response.content)
fc_options=Options()
fc_options.add_argument('--headless')
driver=webdriver.Chrome(chrome_options=fc_options)
# driver=webdriver.PhantomJS()
driver.get('https://www.taobao.com/')
search=driver.find_element_by_class_name('search-combobox-input-wrap')
action_a=ActionChains(driver)
action_a.move_to_element(search).click().send_keys(key_word).send_keys(Keys.RETURN).perform()
driver.switch_to_window(driver.window_handles[-1])
time.sleep(2)
count=0
flag=False
while True:
try:
next=driver.find_element_by_css_selector("[class='item next next-disabled']")
flag=True
except:
pass
img_element=driver.find_elements_by_xpath(".//div[@class='item J_MouserOnverReq ']//a/img")
img_urls=[]
threads=[]
for i in img_element:
img_url = i.get_attribute('data-src')
img_urls.append(img_url)
for ind,i in enumerate(img_urls):
ind=ind+count
f=threading.Thread(target=down_img,args=(key_word,ind,i,headers))
threads.append(f)
count += len(img_urls)
for i in threads:
i.start()
if flag:
break
nt=driver.find_element_by_css_selector("[class='item next']")
nt.click()
driver.switch_to_window(driver.window_handles[-1])
time.sleep(5)
driver.close()
driver.quit()