• 欢迎访问小杰博客网站
  • 欢迎访问小杰博客网站哦

python批量抓取名字

未分类 小杰 8年前 (2015-09-06) 880次浏览 已收录 0个评论

由于要取名字,需要从名字网站里面批量抓取名字备用:

# coding:utf-8

import sys
import json
import os
import shutil
import MySQLdb
import math

sys.path.append(“..”)
from log import *
from http import Http

class Nick:
    def __init__(self):
        self.nick_names = None
        
    #解析网页中的昵称列表
    def parse_nick_names(self):
        print “fond ul”
        finder = “name_show\”>”
#        print self.nick_content
        i = self.nick_content.find(finder)
        if(i == -1):
            return False
        nick_names = self.nick_content[i+len(finder):]
        print “fond /ul”
        i = nick_names.find(“<div style=\”clear:both”)
        if(i == -1):
            return False
        nick_names = nick_names[0:i]
                
        nick_names = nick_names.replace(“&amp;”, “&”)
        nick_names = nick_names.replace(“<li>”, “”)
#        print “nick_names: ”, nick_names
        Log.logCrlf(“nick_names”)
#        Log.logCrlf(nick_names)
#        nick_names.encode(“gb2312”)
        nick_names = nick_names.split(“</li>”)
        del(nick_names[len(nick_names)-1])
        Log.logCrlf(‘,’.join(nick_names))

        return nick_names
    
    def get_nick_name(self):
        print “get_nick_name start”
        Http.init()
        data = {“surname”:”rand”, “sex”:”all”, “num”:”10000″}
        users_json = Http.post(“www.qmsjmfb.com”,data)
#        print(users_json)
        #Log.logCrlf(users_json)
        self.nick_content = users_json
        return self.parse_nick_names()
        
    def replace_into_data(self,nick_names):
        if(not nick_names):
           print “nick_names is null”
#           exit(0)
        
        conn=MySQLdb.connect(host=”localhost”,user=”root”,passwd=””,db=”brush”,charset=’utf8′)
        print conn
        cursor = conn.cursor()
        
        i=0
        lenth = len(nick_names)
        for i in range(lenth):
            if i%100 == 0:
                print i
                Log.logCrlf(str(i))
            sql = “select 1 from user_nick where nick_name = ‘”+nick_names[i]+”‘ limit 1”
            sel_result = cursor.execute(sql)
            if(sel_result):
                print “sql select has exist:”+sql
                continue
            
            sql = “replace into user_nick (nick_name,created_stamp,last_updated_stamp)values(‘”+nick_names[i]+”‘,now(),now())”
            try:
                exe_result = cursor.execute(sql)
            except:
                print “sql replace has exist:”+sql
#            print exe_result
#            print sql
#            Log.logCrlf(sql)
            
        Log.logCrlf(“end of sql”)
        conn.commit()
        cursor.close()
        conn.close()

if __name__ == ‘__main__’:       
    reload(sys) 
    sys.setdefaultencoding( “utf-8” )
    Log.create_instance(“get_nick.txt”)
    nick = Nick()
    nick_names = nick.get_nick_name()
    
    if(not nick_names):
        print “nick_names is null”
    #    exit(0)
    else:
        print “nick_names is ok”
        
    nick.replace_into_data(nick_names)

#os.system(“pause”)


小杰博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python批量抓取名字
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址