You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.0 KiB

from info import username,password,password2
from selenium.webdriver.common.by import By
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
import smtplib
import time
from bs4 import BeautifulSoup
from email.mime.text import MIMEText
from email.header import Header
def crawl():
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "localhost:9222") #此处端口保持和命令行启动的端口一致
#使用ChromeDriver创建一个浏览器实例。
driver = Chrome(options=chrome_options)
driver.get('https://login.taobao.com')
# 等待页面加载完成
time.sleep(2)
# # 初次登录
# # 输入用户名和密码
# driver.find_element(By.ID,'fm-login-id').send_keys(username)
# driver.find_element(By.ID,'fm-login-password').send_keys(password)
#
# time.sleep(4)
# # 点击登录按钮
# driver.find_element(By.CLASS_NAME,'fm-button.fm-submit.password-login').click()
# #多次登陆
# driver.find_element(By.ID,'fm-login-password').send_keys(password)
# time.sleep(3)
# driver.find_element(By.CLASS_NAME,'fm-button.fm-submit.password-login').click()
#快速登陆
driver.find_element(By.CLASS_NAME,'fm-btn').click()
#等待登录完成
time.sleep(10)
# 搜索指定商品
driver.find_element(By.CLASS_NAME,'search-combobox-input').send_keys('手机')
time.sleep(2)
driver.find_element(By.CLASS_NAME,'btn-search').click()
#切换到新窗口
handles=driver.window_handles
driver.switch_to.window(handles[-1])
time.sleep(3)
#数据提取,打印
soup=BeautifulSoup(driver.page_source,'html.parser')
title=[]
price=[]
titles=soup.find_all('div',class_='Title--title--jCOPvpf')
for t in titles:
title.append(t.text)
prices=soup.find_all('div',style="margin-right: 8px;")
for p in prices:
price.append(p.text)
print('{:^40}\t\t\t{:^2}'.format('商品名称','价格',chr(12288)))
for i in range(len(price)):
print('{:<20}\t{:<5}'.format(title[i],price[i]))
return title,price
def send_email(content):
account = username #邮箱地址
password = password2 #邮箱授权码
receiver = username #收件人
message = MIMEText(content,'plain', 'utf-8')
subject = ('淘宝数据')
message['Subject'] = Header(subject)
message['From'] = Header(account) # 设置发件人地址
message['To'] =Header(receiver)
try:
server = smtplib.SMTP_SSL('smtp.qq.com', 465)
server.login(account, password)
server.sendmail(account, [receiver], message.as_string())
print('发送成功')
server.quit()
except Exception as e:
print ('邮件发送失败')
print(e)
def main():
title,prices= crawl()
temp=''
for i in range(len(prices)):
temp=temp+title[i]+'\t'+str(prices[i])+'\n'
send_email(temp)
main()