Python京东爬虫

Python京东爬虫

这是我以前写的一个基于Chrome浏览器的京东爬虫,使用了selenium库和Chrome浏览器,实验性质的脚本,可以根据不同的商品名称,抓取京东商城上的商品明细列表,并存入MySQL数据库。

京东爬虫的Github项目地址

1. 安装对应的ChromeDriver

ChromeDriver下载镜像链接

2. 安装Python3

python3安装教程

3. 安装Mysql8.0数据库

Docker安装MySQL5.7和8

4. 执行SQL脚本创建数据库
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS `jdshop`;
CREATE TABLE `jdshop`  (
  `sid` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品ID',
  `keyword` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '搜索词',
  `shop` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '店铺名称',
  `label` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '前置标签',
  `title` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品标题',
  `advertising` varchar(1000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '广告',
  `price` decimal(8, 2) NULL DEFAULT NULL COMMENT '价格',
  `pinggou` varchar(40) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '拼购价',
  `plus` decimal(8, 2) NULL DEFAULT NULL COMMENT '会员价',
  `comment` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '评论数',
  `tag` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '打标',
  `isale` int(11) NULL DEFAULT NULL COMMENT '销量排名',
  `ctime` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`sid`) USING BTREE,
  UNIQUE INDEX `sid_UNIQUE`(`sid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 6001 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;
5. 修改链接字符串和商品名称
# 待抓取的商品名称
KEYWORD = '手机'
# mysql 连接字符串
conn = pymysql.connect(host='192.168.72.128', port=3306, user='admin', passwd='XXXXXX', db='dahlindb')
6. 执行脚本源代码
import re
import time
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from urllib.parse import quote
from pyquery import PyQuery as pg
import json
import csv
from bs4 import BeautifulSoup
import pymysql

INDEX = 0
CREATE_TIME = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
KEYWORD = '手机'
URL = 'https://search.jd.com/Search?keyword={key1}&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq={key2}&page={page}&s={amount}&psort=3&cid2=653&cid3=655&click=0'
MAX_PAGE = 100

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(options=chrome_options)
wait = WebDriverWait(browser, 10)

conn = pymysql.connect(host='192.168.72.128', port=3306, user='admin', passwd='XXXXX', db='dahlindb')
sql_insert = "INSERT INTO jdshop (keyword,shop,label,title,advertising,price,pinggou,plus,comment,tag,isale,ctime) " \
             "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);"
cursor = conn.cursor()


def filter_title(thtml):
    """
    Extract tags and titles
    :param thtml:
    :return:
    """
    label, title = '', ''
    front_title = re.search('<span.*?>(.*?)</span>', thtml, re.S)
    if front_title and len(front_title.group()) > 1:
        label = front_title[1]
    result = re.search('<em>(.*?)</em>', thtml, re.S)
    temp = re.sub('<span.*?</span>', '', result[1])
    temp = re.sub('<font.*?>', ' ', temp)
    temp = re.sub('</font>', '', temp)
    title = re.sub('<img.*?/>', ' ', temp)
    return label, title


def index_page(index):
    """
    Grab page data based on index code
    :param page:index
    :return:
    """
    try:
        amount = 1
        page = 1
        if index > 1:
            amount = (index-1)*60+1
            page = index+2
        print("正在扒取第{page}页".format(page=index))
        url = URL.format(key1=quote(KEYWORD), key2=quote(KEYWORD), page=page, amount=amount)
        browser.get(url)
        for i in range(1, 5):
            browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(1)
            wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR, '.gl-item'))
            )
        html = browser.page_source
        get_products(html)
    except TimeoutException as e:
        print(e)

def get_products(html):
    """
    Extract product detail data
    :return:
    """
    global INDEX
    doc = pg(html)
    items = doc('.gl-item').items()
    for item in items:
        pinggou = item.find('.price-pingou').text().replace('\n', ' ').strip('¥')
        INDEX = INDEX+1
        thtml = item.find('.p-name').html()
        label, title = filter_title(thtml)
        if item.find('.p-tag3').attr('src'):
            label = '京东精选'
        shop = item.find('.p-shop').text().strip('\n')
        advertising = item.find('.promo-words').text()
        plus = item.find('.price-plus-1').text().strip('¥')
        comment = item.find('.p-commit').text().replace('\n条评价', '').strip('二手有售').strip('\n')
        tag = item.find('.p-icons').text().replace('\n', '-')
        sprice = BeautifulSoup(item.find('.p-price').html(), 'lxml')
        price = sprice.i.string
        plus = plus if plus != '' else price
        if not (price.split('.')[0]).isdigit():
            price = "0"
            plus = "0"
        insert_db = (KEYWORD, shop, label, title, advertising, price, pinggou, plus, comment, tag, INDEX, CREATE_TIME)
        print(insert_db)
        try:
            effect_now = cursor.executemany(sql_insert, [insert_db, ])
            conn.commit()
        except Exception as e:
            print(e)
        print('The {} is running,return value is  {}! '.format(INDEX, cursor.lastrowid))

def main():
    """
    Traversing a hundred pages of data
    :return:
    """
    for i in range(1, MAX_PAGE+1):
        index_page(i)
        time.sleep(2)
    browser.close()
    cursor.close()
    conn.close()

if __name__ == '__main__':
    main()
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值