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()
Selenium、beautifulsoup爬取淘宝数据
最新推荐文章于 2020-12-10 13:00:40 发布