03 Selenium 实战 爬取京东商品

Posted primice

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了03 Selenium 实战 爬取京东商品相关的知识,希望对你有一定的参考价值。

Selenium和Requests都是Python中常用的网络请求库,但是Selenium获取数据的方式与Requests有些差别,
Selenium可以直接模拟浏览器操作,获取数据更加方便,但是相应的速度也会慢一些。

下面是使用selenium获取京东商品数据的示例代码:

首先,导入selenium库中的webdriver模块,然后创建一个Edge浏览器对象

from selenium import webdriver

driver = webdriver.Edge()

接下来,使用get()方法请求网页并全屏打开,这里以京东首页为例:

driver.get("https://www.jd.com")
driver.maximize_window()

然后,使用find_element_by_xpath()方法获取搜索框的节点,再使用send_keys()方法输入要搜索的商品名称:

driver.find_element_by_xpath(\'//input[@id="key"]\').send_keys(\'python\')

接下来,使用click()方法点击搜索按钮:

driver.find_element_by_class_name(\'button\').click()

然后,使用find_element_by_xpath()方法获取商品列表的节点,再获取所有商品的节点,
接下来,使用for循环遍历所有商品的节点,然后获取商品的名称、价格、链接等信息:
由于京东中有一个触底动态加载的功能,所以需要先将页面滚动到底部,等商品加载完成后再获取商品信息:

driver.execute_script(\'window.scrollTo(0, document.body.scrollHeight)\')
lis = driver.find_elements_by_xpath(\'//ul[@class="gl-warp clearfix"]/li\')
for li in lis:
    goods[\'sku\'] = li.get_attribute(\'data-sku\')  # 获取商品的sku
    goods[\'title\'] = li.find_element_by_class_name(\'p-name\').text # 获取商品的标题
    goods[\'price\'] = li.find_element_by_class_name(\'p-price\').text # 获取商品的价格
    goods[\'commit\'] = li.find_element_by_class_name(\'p-commit\').text # 获取商品的评论数
    goods[\'shop\'] = li.find_element_by_class_name(\'p-shop\').text # 获取商品的店铺
    print(goods)

最后,使用quit()方法关闭浏览器:

driver.quit()

完整代码如下:

from selenium import webdriver

driver = webdriver.Edge()
driver.get("https://www.jd.com")
driver.maximize_window()
driver.find_element_by_xpath(\'//input[@id="key"]\').send_keys(\'python\')
driver.find_element_by_class_name(\'button\').click()
driver.execute_script(\'window.scrollTo(0, document.body.scrollHeight)\')
lis = driver.find_elements_by_xpath(\'//ul[@class="gl-warp clearfix"]/li\')
for li in lis:
    goods = 
    goods[\'sku\'] = li.get_attribute(\'data-sku\')  # 获取商品的sku
    goods[\'title\'] = li.find_element_by_class_name(\'p-name\').text # 获取商品的标题
    goods[\'price\'] = li.find_element_by_class_name(\'p-price\').text # 获取商品的价格
    goods[\'commit\'] = li.find_element_by_class_name(\'p-commit\').text # 获取商品的评论数
    goods[\'shop\'] = li.find_element_by_class_name(\'p-shop\').text # 获取商品的店铺
    print(goods)
driver.quit()

运行结果如下:

\'sku\': \'10037672406859\', \'title\': \'Python编程实战100例 (微课视频版)chatg...\'
\'sku\': \'11993134\', \'title\': \'Python编程 从入门到实践 第3版(图灵出品) Pyth...\'
\'sku\': \'12842874\', \'title\': \'Python编程三剑客新版:Python编程从入门到实践第...\'
\'sku\': \'12353915\', \'title\': \'零基础学Python(Python3.10全新升级)(基础入门 ...

下面尝试用面向对象的方式来实现上面的代码:

from selenium import webdriver
import time

class JdSpider(object):
    def __init__(self):
        self.driver = webdriver.Edge()
        self.url = \'https://www.jd.com\'

    def scroll_page_to_bottom(self):
        self.driver.execute_script(\'window.scrollTo(0, document.body.scrollHeight)\')

    def get_page(self):
        self.driver.get(self.url)
        self.driver.maximize_window()
        self.driver.find_element_by_id(\'key\').send_keys(\'python\')
        self.driver.find_element_by_class_name(\'button\').click()
        self.driver.execute_script(\'window.scrollTo(0, document.body.scrollHeight)\')

        time.sleep(3)
        self.scroll_page_to_bottom()
        time.sleep(3)
        self.parse_page()

    def parse_page(self):
        lis = self.driver.find_elements_by_xpath(\'//ul[@class="gl-warp clearfix"]/li\')
        for li in lis:
            goods = 
            goods[\'sku\'] = li.get_attribute(\'data-sku\')  # 获取商品的sku
            goods[\'img\'] = li.find_element_by_class_name(\'p-img\').get_attribute(\'src\') # 获取商品的图
            goods[\'title\'] = li.find_element_by_class_name(\'p-name\').text # 获取商品的标题
            goods[\'price\'] = li.find_element_by_class_name(\'p-price\').text # 获取商品的价格
            print(goods)

    def main(self):
        self.get_page()
        self.driver.quit()

if __name__ == \'__main__\':
    jd = JdSpider()
    jd.main()

Python爬虫实战:爬取京东商品列表

1,引言

在上一篇Python爬虫实战:爬取Drupal论坛帖子列表》,爬取了一个用Drupal做的论坛,是静态页面,抓取比较容易,即使直接解析html源文件都可以抓取到需要的内容。相反,JavaScript实现的动态网页内容,无法从html源代码抓取需要的内容,必须先执行JavaScript。


我们在《
Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容》一文已经成功检验了动态网页内容的抓取方法,
本文将实验程序进行改写,使用开源Python爬虫规定的标准python内容提取器,把代码变得非常简洁。

2,技术要点


我们在多个文章说过本开源爬虫的目的:节省程序员的时间。关键是省去编写提取规则的时间,尤其调试规则很花时间,节省时间问题在《1分钟快速生成用于网页内容提取的xslt
》一文已经有了解决方案,本文我们用京东网站作为测试目标,而电商网站都有很多动态内容,比如,产品价格和评论数等等,往往采用后加载的方式,在html源文档加载完成以后再执行javascript代码把动态内容填写上,所以,本案例主要验证动态内容的抓取。

另外,本文案例没有使用GooSeeker爬虫API,而是把MS谋数台生成的xslt脚本程序保存在本地文件中,在程序运行的时候把文件读出来注入到gsExtractor提取器。后续会有专门的案例演示 API的使用方法。


总之,本示例两个技术要点总结如下:

  • 从本地文件读取xlst程序
  • 把xlst注入到提取器gsExtractor中,利用xslt从网页上一次提取性多个字段内容。


3,python源代码

源代码下载位置请看文章末尾的GitHub源。

4,抓取结果


运行上面的代码,就会爬取京东手机品类页面的所有手机型号、价格等信息,并保存到本地文件“京东手机列表_1.xml”中。我们用浏览器打开这个结果文件,会看到如下的内容。


5,相关文档

1, Python即时网络爬虫项目: 内容提取器的定义

6,集搜客GooSeeker开源代码下载源
1, GooSeeker开源Python网络爬虫GitHub源

7,文档修改历史
1,
2016-06-11:V1.0

以上是关于03 Selenium 实战 爬取京东商品的主要内容,如果未能解决你的问题,请参考以下文章

Python爬虫实战:爬取京东商品列表

通过selenium实现的京东商品爬取

网络爬虫-爬取京东商品评价数据

Python 不用selenium 带你高效爬取京东商品评论

爬虫(十七):Scrapy框架 对接selenium爬取京东商品数据

爬取京东评论信息