使用bs4對海投網內容資訊進行提取並存入mongodb資料庫

來源:互聯網
上載者:User

標籤:

 example:    http://xyzp.haitou.cc/article/722427.html

首先是直接下載好每個頁面,可以使用 os.system( "wget "+str(url))  或者urllib2.urlopen(url) ,很簡單不贅述。

然後,重頭戲,進行資訊抽取:

#!/usr/bin/env python# coding=utf-8from bs4 import BeautifulSoupimport codecsimport sysimport osreload(sys)sys.setdefaultencoding("utf-8")import refrom pymongo import MongoClientdef get_jdstr(fname):    soup = ""    retdict = {}    with open(fname) as fr:        soup = BeautifulSoup(fr.read().replace(‘""‘,‘"‘))        jdstr = soup.get_text()        retdict["inc_name"] = soup.title.string.split()[0]    retdict["page_content"] = soup.find_all("div","panel-body panel-body-text")[0].get_text()    retdict["index_url"] = re.search("http://xyzp.haitou.cc/article/\d+.html",jdstr).group()    retdict["info_from"] = soup.find_all("p","text-ellipsis")[0].contents[1].get_text()    retdict["workplace"] = soup.find_all("p","text-ellipsis")[1].contents[1].get_text()    retdict["info_tag"] = soup.find_all("p","text-ellipsis")[2].contents[1].get_text()    retdict["pub_time"] = soup.find_all("p","text-ellipsis")[3].contents[1].get_text()    return retdictdef JD_extr():    fnames = [ fname  for fname in os.listdir("./") if fname.endswith(".html") ]    fw = codecs.open("tmp_jd_haitou_clean.csv","w","utf-8")    res = []    for fname in fnames[1:500]:        tmp = []        retdict =  get_jdstr(fname)        res.append(retdict)        for k,v in retdict.iteritems():            tmp.append(v)        fw.write(" , ".join(tmp)+"\n")        fw.write("==="*20+"\n")     print fname,"done!"    return resdef change2html():    fnames = [ fname for fname in os.listdir("./") if fname.endswith(".txt") ]    for fname in fnames:        cmd = "mv "+str(fname) +" "+fname[:-3]+"html"        print cmd        os.system(cmd)def store2mongodb():    client = MongoClient("localhost",27017)    db = client.JD_Haitou        documents = JD_extr()    for d in documents:        db.haitouJD.insert(d)    mycol = db["haitouJD"]    print mycol.count()def split_jd_test_data(fname=‘./tmp_jd_haitou_clean.csv‘):    fw = codecs.open(‘./split_jd_res.csv‘,‘w‘,‘utf-8‘)    fr = codecs.open(fname,‘r‘,‘utf-8‘)    indexurl = re.compile("http://xyzp.haitou.cc/article/\d+.html")    for line in fr:         if indexurl.search(line):            url = indexurl.search(line).group()            cnt = ‘1‘  #預設為1            fw.write(url+"\t"+cnt+"\n")    fr.close()    fw.close()if __name__ == "__main__":   JD_extr()  # 抽取後存入檔案    store2mongodb()    split_jd_test_data()    print "done"

 

使用bs4對海投網內容資訊進行提取並存入mongodb資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.