python关键词大全_Python 批量获取Baidu关键词的排名并入库

1.[代码][Python]代码

#-*- coding: UTF-8 -*-

#Python UTF-8 抓取百度关键词V1.0

#key.txt是抓取文件配置

#author PHPer.yang@gmail.com

import cgi,urllib #URL读取

import re #正则匹配

import MySQLdb #MySQL

import datetime #时间

#import time,thread #多线程

"""

MySQL表结构

CREATE TABLE `baidu` (

`id` int(10) unsigned NOT NULL auto_increment,

`url` varchar(200) NOT NULL,

`title` varchar(600) NOT NULL,

`keys` varchar(100) NOT NULL,

`bdurl` varchar(200) NOT NULL,

`date` date NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

"""

def Yang_Config ():

fp = open('key.txt','r')

for line in fp.read().split('@'):

word = line.split(',') #word 是字典

#for item in word :

#print item.encode("UTF-8")

#print '------'

if len(word) > 1:

yang_u = word[0]

yang_k = word[1]

Yang_Spider(yang_u,yang_k)

#抓取页面开始

def Yang_Spider(yang_u,yang_k):

url = 'http://www.baidu.com/s?wd=%s+site:%s&&rn=100'% (yang_k,yang_u)

print url

fp = urllib.urlopen(url).read()

#print fp re.search

m = re.findall(r"

()?(.*?)\s*?.*?.*? ((\d{4}\-\d{1,2}\-\d{1,2})|(\d+小时前)|(\d+分钟前)) .*?.*?
",fp)

if m:

#print m #

for s in m:#数组抓取过来是gbk 转码成utf8.encode("UTF-8") 是汉字decode('gbk') ASNII转UTF8 入数据库操作print str(s[3]) #

print '~~~'.join(s) #切割数组

Yang_MySQL (yang_k,yang_u,s)

#入库

#for i, s in enumerate(m.group(3)):

#print i,s

else:

print 'not search'

def Yang_MySQL (k,u,s):

global cursor,d

cursor.execute("set names utf8")

key_unicode = s[3].decode('gb2312') #gb2312

key_utf8 = key_unicode.encode('utf-8')

SQL = " INSERT INTO `baidukey`.`baidu` (`url` ,`title` ,`keys` ,`bdurl` ,`date`) VALUES ('%s', '%s', '%s','%s','%s'); " % (s[2],key_utf8,k,u,d)

insert = cursor.execute(SQL)

#print SQL

#运行抓取函数

conn = MySQLdb.connect(host="localhost",user="phper",passwd="123456",db="baidukey")

cursor = conn.cursor()

t = datetime.datetime.now()

d = t.strftime('%Y-%m-%d')#%H:%M:%S

Del = " DELETE FROM `baidukey`.`baidu` WHERE date = '%s'" % (d)

cursor.execute(Del)

Yang_Config()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值