使用selenium爬取淘宝商品信息

使用selenium爬取淘宝商品信息

1. 发送请求

driver = webdriver.Chrome()
# 打开某些网页,会有广告遮住找到的tag,设置窗口的大小就可以避免
# driver.set_window_size(1920, 1080)
driver.implicitly_wait(6)
url = 'https://www.taobao.com/'
driver.get(url)

2. 输入商品名称,并且自动搜索

定位到输入框,设置想输入的信息,我这里以iPad为例

然后定位到搜索按钮的位置,设置点击

driver.find_element_by_xpath('//*[@id="q"]').send_keys('iPad')
driver.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button').click()

3. 解析出需要爬取的数据

通过xpath找到单个商品所有信息的div

遍历并用xpath解析出需要的数据,我这里只取出了商品信息,价格,店铺这三个信息,解析其他信息使用同样的方法即可

注:解析出的信息为string类型,如有特殊字符,定义方法清洗即可

divs = driver.find_elements_by_xpath('//div[contains(@class, "J_MouserOnverReq")]')
    for div in divs:
        shop = div.find_element_by_xpath('.//div[2]/div[3]/div[1]').text
        goods = div.find_element_by_xpath('.//div[2]/div[2]/a').text.strip()
        price = div.find_element_by_xpath('.//div[2]/div[1]/div[1]').text

4. 循环遍历下一页数据

定位到“下一页”按钮的a标签,这里需要注意,如果是最后一页,“下一页”的a标签并不能找到,会报NoSuchElementException错误,这时候捕捉异常,然后用break结束循环即可

有些同学可能会报selenium.common.exceptions.StaleElementReferenceException: Message: stale eleme这个错误,这是因为页面还没加载完,通过xpath解析不出来“下一页”的标签,这时候就会报错。解决办法有几个:

  1. 刷新页面几乎是万能的方法driver.refresh(),但有些情况不允许刷新页面,就用不了了
  2. 设置等待时间time.sleep(2),这个方法不是每次都会奏效的
  3. 捕捉异常StaleElementReferenceException,然后重新获取元素,此方法也比较靠谱
while True:
	try:
        next_page = driver.find_element_by_xpath('//a[contains(@trace, "srp_bottom_pagedown")]')
    except NoSuchElementException as e:
        print('爬取完毕!')
        break
    else:
        time.sleep(4)
        print('第{}页爬取完毕'.format(driver.find_element_by_xpath('//li[@class="item active"]').text))
        next_page.click()
        time.sleep(3)
        driver.refresh()

5. 最后附上完整代码

我是将爬到的数据直接写入一个表格中了,并没有写入数据库,写入数据库的操作,我觉得并不难,并不是我想偷懒

from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
import csv

driver = webdriver.Chrome()
# driver.set_window_size(1920, 1080)
driver.implicitly_wait(6)
url = 'https://www.taobao.com/'
driver.get(url)

driver.find_element_by_xpath('//*[@id="q"]').send_keys('iPad')
driver.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button').click()
with open('iPad.csv', 'a', newline='', encoding='utf-8') as f:
    writer = csv.writer(f)
    writer.writerow(('商品', '价格', '店铺'))

while True:
    divs = driver.find_elements_by_xpath('//div[contains(@class, "J_MouserOnverReq")]')
    for div in divs:
        shop = div.find_element_by_xpath('.//div[2]/div[3]/div[1]').text
        goods = div.find_element_by_xpath('.//div[2]/div[2]/a').text.strip()
        price = div.find_element_by_xpath('.//div[2]/div[1]/div[1]').text
        with open('iPad.csv', 'a', newline='', encoding='utf-8') as f:
            writer = csv.writer(f)
            writer.writerow((goods, price, shop))

    try:
        next_page = driver.find_element_by_xpath('//a[contains(@trace, "srp_bottom_pagedown")]')
    except NoSuchElementException as e:
        print('爬取完毕!')
        break
    else:
        time.sleep(4)
        print('开始爬取第{}页'.format(driver.find_element_by_xpath('//li[@class="item active"]').text))
        next_page.click()
        time.sleep(3)
        driver.refresh()

driver.quit()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值