Python的网络爬虫小系统——爬取京东商城商品信息

import time
from selenium import webdriver
keyword = input('请输入你要搜索的关键字:')
brow = webdriver.Firefox()
brow.get('http://www.jd.com')
search_form = brow.find_element_by_id('key')
search_form.send_keys(keyword)
search_button = brow.find_element_by_class_name('button')
search_button.click()
time.sleep(2)
file_handle = open('%s.txt'%keyword,mode='w',encoding='utf-8')
for x in range(1,6):
    print('正在爬取第%s页数据,请稍等'%x)
    for x in range(1,11,2):
        time.sleep(1)
        j = x / 10
        js = 'document.documentElement.scrollTop = document.documentElement.scrollHeight * %f' % j
        brow.execute_script(js)
    shops = brow.find_elements_by_class_name('gl-item')
    for shop in shops:
        file_handle.write(shop.text)
    next_li = brow.find_element_by_class_name('pn-next')
    next_li.click()
brow.quit()

你可能感兴趣的:(Python的网络爬虫小系统——爬取京东商城商品信息)