日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2025/3/12 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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()

總結

以上是生活随笔為你收集整理的python关键词大全_Python 批量获取Baidu关键词的排名并入库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。