From 72340649666ad2fc818a90d91ec520414e644f0f Mon Sep 17 00:00:00 2001 From: Nyloner Date: Wed, 21 Sep 2016 21:13:50 +0800 Subject: [PATCH] brokerbin --- brokerbin.com/brokerbin.py | 116 +++++++++++++++++++++++++++++++++++ brokerbin.com/email_template | 49 +++++++++++++++ brokerbin.com/send_email.py | 46 ++++++++++++++ 3 files changed, 211 insertions(+) create mode 100644 brokerbin.com/brokerbin.py create mode 100644 brokerbin.com/email_template create mode 100644 brokerbin.com/send_email.py diff --git a/brokerbin.com/brokerbin.py b/brokerbin.com/brokerbin.py new file mode 100644 index 0000000..8060076 --- /dev/null +++ b/brokerbin.com/brokerbin.py @@ -0,0 +1,116 @@ +import requests +from bs4 import BeautifulSoup +import time +import re +import json + +headers = { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate", + "Accept-Language": "en-US,en;q=0.5", + "Connection": "keep-alive", + "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0"} + + +email_template=open('email_template','r',encoding='utf-8').read() + +def login(): + session=requests.session() + data={ + 'login':"", + 'password':"", + 'remember':"1" + } + session.post('http://members.brokerbin.com/',data=data,headers=headers,timeout=30) + return session + +def get_form(session): + platform='CISCO' + html=session.get('http://members.brokerbin.com/main.php?platform={}&refresh=%27.%24submit.%27&loc=top100&view=1'.format(platform),headers=headers,timeout=30).text + table=BeautifulSoup(html,'html.parser').find('table',{'class':'rowHighlight'}).find_all('tr',{'class':'pref_vendor'}) + result=[] + for item in table: + try: + url='http://members.brokerbin.com/'+item.find('a').get('href') + result.append(url) + except: + continue + return result + +def get_person(session,url): + html=session.get(url,headers=headers,timeout=30).text + table=BeautifulSoup(html,'html.parser').find('table',{'class':'nowrap'}).find_all('tr',{'class':'caution'}) + result=[] + for item in table: + try: + tds=item.find_all('td') + if 'Hrs' not in tds[1].get_text(): + continue + product=tds[2].get_text().replace('\t','').replace(' ','').replace('\xa0','') + person_name=item.find('a').get_text() + person_url=re.findall("newWindow\('(.*?),\d",item.find('a').get('onclick'))[0] + result.append({'product_name':product,'person_name':person_name,'person_url':person_url}) + except: + continue + return result + +def get_email(session,url): + html=session.get('http://members.brokerbin.com/'+url,headers=headers,timeout=30).text + email=re.findall('"mailto:(.*?)"',html)[0] + return email + +def get_price(product_name): + html=requests.get('http://sailnetwork-cc.3dcartstores.com/search_quick.asp?q='+product_name,headers=headers,timeout=30).text + data=json.loads(html)[0]['results'] + for item in data: + if item[1]==product_name: + return item + return data[0] + +def write_to_email_txt(item): + text=email_template.format(name=item['person_name'],product_name=item['product_name'] + ,price=item['product'][3],product_url='http://sailnetwork-cc.3dcartstores.com/'+item['product'][0]) + date=time.strftime('%Y%m%d') + f=open(date+'.txt','a',encoding='utf-8') + f.write('qoute for %s\r\n'%(item['product_name'])+'***'*4+'\r\n'+item['email']+'\r\n'+'***'*4+'\r\n'+text+'\r\n'+'---'*8+'\r\n\r\n') + f.close() + +def main(): + session=login() + urls=get_form(session) + persons=[] + for url in urls: + result=get_person(session,url) + persons+=result + print(url,'ok') + result=[] + price={} + for person in persons: + try: + email=get_email(session,person['person_url']) + except: + continue + person['email']=email + try: + product_price=price[person['product_name']] + person['product']=product_price + write_to_email_txt(person) + except: + print(person['product_name'],'get price failed') + failed=open('failed','a',encoding='utf-8') + failed.write(str(person)+'\r\n') + failed.close() + continue + pass + try: + product_price=get_price(person['product_name']) + except: + print(person['product_name'],'get price failed') + failed=open('failed','a',encoding='utf-8') + failed.write(str(person)+'\r\n') + failed.close() + continue + price[person['product_name']]=product_price + person['product']=product_price + write_to_email_txt(person) +main() diff --git a/brokerbin.com/email_template b/brokerbin.com/email_template new file mode 100644 index 0000000..41e647b --- /dev/null +++ b/brokerbin.com/email_template @@ -0,0 +1,49 @@ + + + + + + + +

+ Hi {name}, +

+

+ We have following quote matches your "searching" on Brokerbin: +

+

+ {product_name} new landed {price} each 3days delievery +

+

+ + Click here to buy on our website. + +

+

+ Please know the special price only provide after you logined. +

+

+ Thanks! +

+

+

+

+ -- +

+

+ *Please register at our web-store:www.sailnetwork.com to check P&A 24-7. Regular coupons will be sent to register customers, and product prices will be lower online. +
+ Best regards; +
+ Sales Department | Sail Network Co., Ltd. +
+ Office: +86(0)2154223056*8004 +
+ E-mail: sales@sailnetwork.com +
+ E-Shop: www.sailnetwork.com +
+ No.3-318, Lane7058, Zhongchun Rd. Shanghai, China +

+ + diff --git a/brokerbin.com/send_email.py b/brokerbin.com/send_email.py new file mode 100644 index 0000000..93da1e7 --- /dev/null +++ b/brokerbin.com/send_email.py @@ -0,0 +1,46 @@ +from email import encoders +from email.header import Header +from email.mime.text import MIMEText +from email.utils import parseaddr,formataddr +import smtplib +import time + +def _format_addr(s): + name, addr = parseaddr(s) + return formataddr((Header(name, 'utf-8').encode(), addr)) + +def sendEmail(fromemail,passwd,toemail,subject,text): + msg = MIMEText(text, 'html', 'utf-8') + msg['Subject']=Header(subject, 'utf-8').encode() + msg['From'] = _format_addr(fromemail) + msg['To'] = _format_addr(toemail) + server=smtplib.SMTP('smtp-mail.outlook.com',587) + server.starttls() + server.login(fromemail,passwd) + server.sendmail(fromemail, [toemail], msg.as_string()) + server.quit() + +def load_emails(): + f=open('./20160921.txt','r',encoding='utf-8').read() + emails=[] + for item in f.split('---'*8): + try: + lines=item.split('***'*4) + subject=lines[0].replace('\r\n','') + email=lines[1].replace('\r\n','').replace(' ','') + text=lines[2] + emails.append([email,subject,text]) + except: + continue + return emails + +def main(): + fromemail='' + passwd='' + emails=load_emails() + for i in range(13,20): + email=emails[i] + sendEmail(fromemail,passwd,email[0],email[1],email[2]) + time.sleep(2) + print(email[1]) +main()