#!/usr/bin/python # -*- coding: UTF-8 -*- from selenium import webdriver from selenium.webdriver.common.keys import Keys import time import sys import pymysql reload(sys) sys.setdefaultencoding('utf-8') driver = webdriver.Firefox(executable_path="/Users/chanming/Desktop/geckodriver") driver.get("https://pay.weixin.qq.com/index.php/partner/public/search") time.sleep(2) st=driver.find_element_by_id('searchPortalText') st.send_keys(u"付") driver.find_element_by_id("searchPortalSubmit").click() time.sleep(3) alist=list() # map={'拓展地域:':'dis','聯 系 人 :':'userName','電 話:':'phone','郵 箱:':'email','地 址:':'address','所屬行業:':'industry'} map={} flag=True for x in xrange(1,23): nextInput=driver.find_element_by_css_selector('.goto-area input') nextInput.clear() nextInput.send_keys(x) nextButton=driver.find_element_by_css_selector('.goto-area a') nextButton.click() time.sleep(1) searchList=driver.find_elements_by_css_selector('#searchResultList dl') for comp in searchList: obj={} obj['title']=comp.find_element_by_tag_name('dt').text propNameList=comp.find_elements_by_class_name('lbl') if flag: map[propNameList[0].text]='dis' map[propNameList[1].text]='userName' map[propNameList[2].text]='phone' map[propNameList[3].text]='email' map[propNameList[4].text]='address' map[propNameList[5].text]='industry' flag=False propList=comp.find_elements_by_class_name('ele') k=0 for propName in propNameList: obj[map[propName.text]]=propList[k].text k=k+1 alist.append(obj) # print len(alist) driver.close() db = pymysql.connect(host = '127.0.0.1', port = 3306, user = 'json', passwd = '123456', db = 'youxia', charset="utf8") cursor = db.cursor() sql = 'insert into wx_pay_company(dis,userName,phone,email,address,industry,title) values(%s,%s,%s,%s,%s,%s,%s)' for company in alist: if not company.has_key('dis'): company['dis']='' if not company.has_key('userName'): company['userName']='' if not company.has_key('phone'): company['phone']='' if not company.has_key('email'): company['email']='' if not company.has_key('address'): company['address']='' if not company.has_key('industry'): company['industry']='' cursor.execute(sql,(company['dis'],company['userName'],company['phone'],company['email'],company['address'],company['industry'],company['title'])) db.commit() cursor.close() db.close() print '運行結束'