天猫官网 - https://www.tmall.com/
Author | Gobi Xu |
---|---|
xusanity@aliyun.com |
- 注册一个淘宝账号
- 注册一个微博账号
- 下载chrome浏览器
- 下载对应chrome浏览器版本的chromedriver驱动
- 全程使用selenium进行爬取
- 请求时需要携带cookies(即需要先登录),否则极易出现验证码
pip install selenium
pip install pyquery
- 防止被检测出为机器
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation']) # 设置为开发者模式
# 在options里加上这个参数使得chromedriver驱动不会被检测出为机器
- 模仿人类行为
for i in range(1, 52):
drop_down = "var q=document.documentElement.scrollTop=" + str(i*100)
self.browser.execute_script(drop_down)
time.sleep(0.01) # 值越小越顺滑,越像人类行为
# 模仿人类,发现喜欢的商品时会停止滑动
if i == 5:
time.sleep(1.3)
if i == 15:
time.sleep(1.9)
if i == 29:
time.sleep(0.7)
if i == 44:
time.sleep(0.3)
slider_button = WebDriverWait(self.browser, 5, 0.5).until(EC.presence_of_element_located((By.ID, 'nc_1_n1z')))
action = ActionChains(self.browser)
action.click_and_hold(slider_button).perform()
action.reset_actions()
# 模拟人类 向左拖动滑块(拖动有加速度)
for i in range(100):
action.move_by_offset(i*1, 0).perform()
time.sleep(0.01) # 值越小越顺滑,越像人类行为
- taobao_spider.py里有大量注释,请放心食用:meat_on_bone:
- 如有任何问题都可以邮箱:email:联系我,我会尽快回复你。