如何通過 Python 挖掘帶流量 Seo 關鍵詞

如何通過 Python 挖掘帶流量 Seo 關鍵詞

方法博客上分享通過百度商情數據接口挖掘關鍵詞的 Shell 。同時,也提供一個 Python 腳本源碼。

在運行腳本之前,請確定你是否按照了 MySQLdb 庫,安裝方法可以去百度一下。

代碼如下:


#!/usr/local/bin/python
#coding:utf8
# 2015-6-26 DaoXin
import pycurl, json, MySQLdb
import StringIO
import urllib, urllib2
from random import choice
import sys 
reload(sys)
sys.setdefaultencoding('utf8')
#useragent 列表,大家可以自行去收集。不過在本例中似乎不需要這個
AGENTS = [
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.27 (KHTML, like Gecko) Chrome/12.0.712.0 Safari/534.27",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.24 Safari/535.1",
            "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.36 Safari/535.7",
            "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0b4pre) Gecko/20100815 Minefield/4.0b4pre",
            "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5",
            "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.17) Gecko/20110123 (like Firefox/3.x) SeaMonkey/2.0.12",
            "Mozilla/5.0 (Windows NT 5.2; rv:10.0.1) Gecko/20100101 Firefox/10.0.1 SeaMonkey/2.7.1",
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; zh-CN) AppleWebKit/532.8 (KHTML, like Gecko) Chrome/4.0.302.2 Safari/532.8",
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; zh-CN) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.464.0 Safari/534.3",
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; zh-CN) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.15 Safari/534.13",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1",
            "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/125.2 (KHTML, like Gecko) Safari/125.8",
            "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; fr-fr) AppleWebKit/312.5 (KHTML, like Gecko) Safari/312.3",
            "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1 Camino/2.2.1",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0b6pre) Gecko/20100907 Firefox/4.0b6pre Camino/2.2a1pre",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.4 (KHTML like Gecko) Chrome/22.0.1229.79 Safari/537.4",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2; rv:10.0.1) Gecko/20100101 Firefox/10.0.1",
            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20120813 Firefox/16.0",
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; zh-CN) AppleWebKit/528.16 (KHTML, like Gecko, Safari/528.16) OmniWeb/v622.8.0.112941",
            "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_6; zh-CN) AppleWebKit/528.16 (KHTML, like Gecko, Safari/528.16) OmniWeb/v622.8.0",
]

UserAgent = choice(AGENTS)


#如果需要把挖出來的關鍵詞保存到數據庫,需要配置數據庫相關信息
class ConnDb():
    global host, user, passwd, db
    host = '111.111.111.111' #數據庫IP
    user = 'python' #數據庫用戶名
    passwd = 'pass' #數據庫密碼
    db = 'dbnamelllllll' # 數據庫名
    def connDb(self):
        global cur
        conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db, port=3306, charset = 'utf8')
        cur = conn.cursor()
        return cur

# 這個curl方法是從zero那里扒過來的。http://www.seoqx.com/post/341
def curl(url, debug=False, **kwargs):
    while 1:
        try:
            s = StringIO.StringIO()
            c = pycurl.Curl()
            c.setopt(pycurl.URL, url)
            c.setopt(pycurl.REFERER, url)
            c.setopt(pycurl.FOLLOWLOCATION, True)
            c.setopt(pycurl.TIMEOUT, 60)
            c.setopt(pycurl.ENCODING, 'gzip')
            c.setopt(pycurl.USERAGENT, UserAgent)
            c.setopt(pycurl.NOSIGNAL, True)
            c.setopt(pycurl.WRITEFUNCTION, s.write)
            for k, v in kwargs.iteritems():
                    c.setopt(vars(pycurl)[k], v)
            c.perform()
            c.close()
            return s.getvalue()
        except:
            if debug:
            	raise
            continue

command = int(raw_input("請選擇導出形式;1:導出為txt,2:導入道數據庫: "))

if command == 1:
	FileWrite = open("output.txt", 'w')
	for line in open('sourceword.txt'):
		kw = str(line)
		jsons = curl('http://honeyimg.bdimg.com/recomword/recomWordCache_findRecomWord.htm?area_id=&word=' + urllib.quote_plus(kw))
		d = json.loads(jsons)
		try:
			dlist = d['data']['list']
			for item in dlist:
				indexs = item['total']
				keywords = item['word'].encode('utf-8')
				outstr = str(indexs) + ',' + str(keywords) + '\n'
				FileWrite.write(outstr)
		except TypeError, e:
			print 'TypeError, Pass', e
			continue
	print 'done to txt'
elif command == 2:
	conndb = ConnDb()
	conndb.connDb()
	for line in open('sourceword.txt'):
		kw = str(line)
		jsons = curl('http://honeyimg.bdimg.com/recomword/recomWordCache_findRecomWord.htm?area_id=&word=' + urllib.quote_plus(kw))
		d = json.loads(jsons)
		try:
			dlist = d['data']['list']
			for item in dlist:
				indexs = item['total']
				#keywords = unicode(item['word'], 'utf-8')
				keywords = item['word'].encode("utf-8")
				sql = "insert into shangqing_keyword (id, total, keyword) values (null, '%s', '%s')"
				try:
					cur.execute(sql % (indexs, keywords))
				except MySQLdb.Error, e:
					print 'MySql error', e
					continue
		except TypeError, e:
			print 'TypeError, Pass' , e
			continue
	print 'done to mysql'
else:
	print '只有兩種導出方式,請輸入1或者2'

使用方法:

1、將你的詞根放到sourceword.txt 中,一行一個詞,然后將本文代碼隨便保存成一個xxxx.py 和sourceword.txt 放在同一個目錄下。

2、交互模式下,進入這兩個文件所在目錄,運行腳本xxx.py(一般是輸入python xxx.py即可)

3、會有提示選擇導出模式,1為導出txt文件,2為導入到mysql中。如下圖

圖中的typeerror請忽略

如果需要導入到mysql中,請配置mysql的相關信息,在代碼中有注釋。

不過首先需要在你的數據庫中創建一個表,語句如下:


CREATE TABLE `shangqing_keyword` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `keyword` varchar(200) DEFAULT NULL,
  `total` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `keyword` (`keyword`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

4、等待完成。

個人認為導入到mysql更好管理,txt比較好處理一些。

我的sourceword中有6000個詞,挖出來差不多30多萬我就停止了。應該能挖出來更多。

效果截圖:

這個是導出為txt格式的樣子

導入到mysql是這樣的。還是mysql用起來順手一些。個人喜好。

注意:導出txt格式是存在重復詞的,因為我不知道怎么去過濾。但是導入到mysql中 重復詞是會自動過濾掉的。不過反正都無所謂,后期處理的時候大家總能找到辦法的。

文 / @方法博客

相關推薦

發表評論

微信掃一掃

微信掃一掃

微信掃一掃,分享到朋友圈

如何通過 Python 挖掘帶流量 Seo 關鍵詞
返回頂部

顯示

忘記密碼?

顯示

顯示

獲取驗證碼

Close
香港开奖直播开奖记录