
现如今,我们已经离不开网上购物,作为阿里的发家之作淘宝成为我们现在手机app中的必备软件,尤其是女生,几乎每天都要打开淘宝浏览商品信息,其实使用python可以通过爬虫可以实现爬取淘宝的商品信息,本文将分步骤向大家介绍Python爬虫爬取淘宝的商品信息的过程。
1、导入需要的包
import timefrom bs4 import BeautifulSoupfrom selenium import webdriver
2、编辑淘宝数据抓取的函数逻辑
# 编辑淘宝数据抓取的函数逻辑 """ 1.登录淘宝 2.首页 3.指定商品的搜索信息 4.提取指定商品的金额、购买人数、商铺地址、商品名称、商品图片 """
3、打开淘宝网页,并扫码登陆点击淘宝网首页
def login_info(self):
# 1.打开网页
self.browser.get(self.url)
# 2.通过扫码的形式去登录淘宝账号
if self.browser.find_element_by_xpath('//*[@id="login"]/div[1]/i'):
self.browser.find_element_by_xpath('//*[@id="login"]/div[1]/i').click()
# 让程序等待休眠5秒,通过手机扫码登录
time.sleep(8)
# 3.点击淘宝网首页
taobao_index = self.browser.find_element_by_xpath('//*[@id="J_SiteNavHome"]/div/a')
taobao_index.click()
time.sleep(1)
4、自动的在淘宝首页中输入自己想要搜索的商品名称,并且自动点击搜索
search_input = self.browser.find_element_by_xpath('//*[@id="q"]')
shop_name = input("请输入你想搜索的商品名称:")
search_input.send_keys(shop_name)
time.sleep(0.5)
search_submit = self.browser.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button')
search_submit.click()
5、获取商品信息
# 商品金额
shop_price_data = shop_data.find_all('div', class_='price g_price g_price-highlight')
for shop_price in shop_price_data:
shop_price_list.append(shop_price.text.strip())
# 购买人数
shop_people_number_data = shop_data.find_all('div','deal-cnt')
for shop_people_number in shop_people_number_data:
shop_people_list.append(shop_people_number.text)
# 店铺地区
shop_location_data = shop_data.find_all('div','location')
for shop_location in shop_location_data:
shop_location_list.append(shop_location.text)
以上就是Python爬虫爬取淘宝的商品信息的分步介绍,大家可以尝试看看哟~
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试



