Python实战项目之模拟浏览器访问京东网站并输入关键字自动搜索

Python实战项目之模拟浏览器访问京东网站并输入关键字自动搜索

1、下载安装PyCharm
下载网址:http://www.jetbrains.com/pycharm/
2、下载chromedriver
下载网址:http://chromedriver.storage.googleapis.com/index.html
3、在PyCharm环境安装selenium库
Python实战项目之模拟浏览器访问京东网站并输入关键字自动搜索_第1张图片
4、创建项目,输入如下代码,大功告成!!

# encoding:utf-8
from selenium import webdriver
# import os
from time import sleep


# 模拟浏览器访问京东并搜索关键字
def jingdong():
    chrome_path = r'E:\chromedriver_win32/chromedriver.exe'
    # os.environ['webdriver.chrome.driver'] = chrome_path#设置系统环境变量
    # 打开谷歌浏览器
    driver = webdriver.Chrome(chrome_path)
    # 打开一个网址#driver.quit() #退出浏览器
    driver.fullscreen_window()
    driver.get('https://www.jd.com/')
    print('Before search================')
    # 打印当前页面title
    title = driver.title
    print(title)
    # 打印当前页面URL
    now_url = driver.current_url
    print(now_url)
    driver.find_element_by_id("key").send_keys("衣服")
    driver.find_element_by_class_name("button").click()
    sleep(1)
    print('After search================')
    # 再次打印当前页面title
    title = driver.title
    print(title)
    # 打印当前页面URL
    now_url = driver.current_url
    print(now_url)
    # # 获取结果数目
    # user = driver.find_element_by_class_name('nums').text
    # print(user)
    # #关闭所有窗口
    # driver.fullscreen_window()
    sleep(5)
    driver.quit()


def main():
    jingdong()


if __name__ == '__main__':
    main()

20190125代码功能添加:提取衣服页面中的商店名字、商品链接、商品价格

from selenium import webdriver
from lxml import etree
import time
KeyWord="衣服"
JDHTTP="https:"
def Jingdong_spider():
    webdriver_path=r"D:\python-tools\chromedriver_win32\chromedriver.exe"
    drivers=webdriver.Chrome(webdriver_path)
    drivers.get("https://www.jd.com/")
    time.sleep(5)
    drivers.find_element_by_id("key").send_keys(KeyWord)
    time.sleep(5)
    drivers.find_element_by_class_name("button").click()
    time.sleep(10)
    html=etree.HTML(drivers.page_source)
    shoplis=html.xpath("//li[@class='gl-item']")
    for shopli in shoplis:
    	#获取商店名字
        shopname=shopli.xpath(".//span[@class='J_im_icon']/a/@title")
        print(shopname[0])
        #获取商品链接
        shoplink=shopli.xpath(".//div[@class='p-img']/a/@href")
        print("http:"+shoplink[0])
        #获取商品价格
        shopprice=shopli.xpath(".//div[@class='p-price']//i")
        print("¥"+shopprice[0].text)

你可能感兴趣的:(python学习总结,Python之网络爬虫)