Selenium、beautifulsoup爬取淘宝数据

import time
from bs4 import BeautifulSoup
import lxml
import re
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

class Taobao_Infos:
	
	def __init__(self):
		url = 'https://login.taobao.com/member/login.jhtml'
		self.url = url
		self.browser = webdriver.Chrome(executable_path='.\chromedriver.exe')
		self.wait = WebDriverWait(self.browser,10)
		
	def login_infos(self):
		self.browser.get(self.url)
		if self.browser.find_element_by_xpath('//*[@id="fm-login-id"]'):
			user = self.browser.find_element_by_xpath('//*[@id="fm-login-id"]')
			user.send_keys('xxx')
			time.sleep(3)
		if 	self.browser.find_element_by_xpath('//*[@id="fm-login-password"]'):
			user = self.browser.find_element_by_xpath('//*[@id="fm-login-password"]')
			user.send_keys('xxxx')
			time.sleep(5)
		login_button = self.browser.find_element_by_xpath('//*[@id="login-form"]/div[4]/button')
		login_button.click()
		time.sleep(1)

		self.browser.find_element_by_xpath('//*[@id="J_SiteNavHome"]/div/a').click()
		time.sleep(1)
		search_box = self.browser.find_element_by_xpath('//*[@id="q"]')
		shop_name = input('请输入你要搜索的商品名称:')
		search_box.send_keys(shop_name)
		time.sleep(1)
		self.browser.find_element_by_xpath('//*[@id="J_TSearchForm"]/div[1]/button').click()
		time.sleep(1)

	def shop_infos(self):
		#商品详细信息
		page = self.browser.page_source
		soup = BeautifulSoup(page,'lxml')
		shop_name_list = []
		shop_price_list = []
		shop_person_list = []
		shop_location_list = []

		soup_data_list = soup.find('div',class_='grid g-clearfix').find_all_next('div',class_='items')
		for shop_data in soup_data_list:
			shop_image_data = shop_data.find_all('div',class_='pic')
			for shop_data_a in shop_image_data:
				shop_data_a = shop_data.find_all('a',class_='pic-link J_ClickStat J_ItemPicA')
				for shop_name in shop_data_a:
					shop_name = shop_name.find_all('img')[0]['alt']
					shop_name_list.append(shop_name)

			#购买价格
			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_purchase_sum = shop_data.find_all('div',class_='deal-cnt')
			for shop_purchase in shop_purchase_sum:
				shop_person_list.append(shop_purchase.text.strip())

			#购买地区
			shop_location_data = shop_data.find_all('div',class_='location')
			for shop_location in shop_location_data:
				shop_location_list.append(shop_location.text.strip())

			#打印产品信息元组
			shop_data = zip(shop_name_list,shop_price_list,shop_person_list, shop_location_list)
			for data in shop_data:
				print(data)


t = Taobao_Infos()
t.login_infos()
t.shop_infos()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值