python爬取企業名錄__python

來源:互聯網
上載者:User

爬取企業名錄然後儲存到資料庫中。

#-*- coding:utf-8 -*-import requestsimport MySQLdbimport refrom bs4 import BeautifulSoupheaders = {    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36"}cookies = {'ASPSESSIONIDQCSRSBRS':'FBEPJPLCHEEMEHNLHFKCBCGB', 'Hm_lvt_819e30d55b0d1cf6f2c4563aa3c36208':'1483118719', 'Hm_lpvt_819e30d55b0d1cf6f2c4563aa3c36208':'1483120442'}db = MySQLdb.connect("localhost","root",'liao1234','liao',charset="utf8")cursor = db.cursor()'''sql ="""create table company(name char(100) not null,    type char(50),    addr char(150),    leader   char(30),    date char(30))"""cursor.execute(sql)'''#擷取各地區連結r = requests.get("http://hangzhou.11467.com/",headers=headers,cookies=cookies)html = r.textsoup = BeautifulSoup(html)for tag in soup.find('div',class_='box sidesubcat t0').find_all('a'):    print tag.attrs['href']    base_url = "http://hangzhou.11467.com"+tag.attrs['href']+'pn'    for i in range(1,10):        url = base_url + str(i)        r1 = requests.get(url,headers=headers,cookies=cookies)        html1 = r1.text        soup1 = BeautifulSoup(html1)        for tag1 in soup1.find('ul',id='slist').find_all('li'):            ss = []            for cc in tag1.find_all('a'):                print cc.string                ss.append(cc.string)            for aa in tag1.find_all('dd'):                print aa.string                ss.append(aa.string)            if len(ss) == 3:                ss.append("none")                ss.append("none")            elif len(ss) == 4:                ss.append("none")            else:                pass            print len(ss)            if len(ss)==0:                continue            sql = "insert into company(name,type,addr,leader,date) values('%s','%s','%s','%s','%s')"%(ss[0],ss[1],ss[2],ss[3],ss[4])            cursor.execute(sql)            print ss[0]db.close()


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.