python 百度cpc點選

BigRain發表於2016-06-27
# coding=utf8
import urllib2
import string
import urllib
import re
import random

#設定多個user_agents,防止百度限制IP
user_agents = ['Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20130406 Firefox/23.0', \
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0', \
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533+(KHTML, like Gecko) Element Browser 5.0', \
'IBM WebExplorer /v0.94', 'Galaxy/1.0 [en] (Mac OS X 10.5.6; U; en)', \
'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)', \
'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14', \
'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25', \
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36', \
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0; TheWorld)']

keywords_address="福州,廈門,深圳,廣州,珠海,佛山,東莞,南昌,九江,上海,杭州,溫州,寧波,石家莊,北京,保定,昆明,成都,南京,".split(',')
keywrods_category="男科醫院,男科病醫院,男性病醫院,男子醫院,包皮過長,包皮手術,包皮手術費用,勃起不好,早洩,男科,早洩手術,陽痿,早洩醫院,早洩費用,前列腺,前列腺醫院,前列腺腫大,不育,不孕不育".split(',')

def baidu_search(keyword,pn):
p= {'wd': keyword} 
req=urllib2.Request(("http://www.baidu.com/s?"+urllib.urlencode(p)+"&pn={0}&cl=3&rn=100").format(pn))
r=random.randint(0,8)
req.add_header('User-agent', user_agents[r])
req.add_header('connection','keep-alive')
res=urllib2.urlopen(req,timeout=10000)
print res.geturl()
html=res.read()
#print res.headers
res.close()
#print "%s\r\n=====html finished ======\r\n"%html
return html
def getCpc(regex,text):
arr = []
res = re.findall(regex, text)
for r in res:
if r not in arr:
arr.append(r)
return arr

def geturl(keyword):

html = baidu_search(keyword,1)

content = unicode(html, 'utf-8','ignore')
arrList = getCpc(u"http://www.baidu.com/baidu.php\?url=[^\',^\"]*", content)
for item in arrList:
url = item
print "\r\n# math url:\r\n %s \r\n"%url
#獲取標題
#title = clearTag(link[1]).encode('utf8')

try:
domain=urllib2.Request(url)
r=random.randint(0,11)
domain.add_header('User-agent', user_agents[r])
domain.add_header('connection','keep-alive')
response=urllib2.urlopen(domain,timeout=10000)#time out 10s
uri=response.geturl()
response.close()
print " *target url: \r\n %s \r\n"%uri
except:
continue

if __name__=='__main__':
categoryCount=len(keywrods_category)
addressCount=len(keywords_address)
for i in range(1,20):
r=random.randint(1,1000)
keyword="%s%s"%(keywords_address[r%addressCount],keywrods_category[r%categoryCount])
print "\r\n%s\r\n"%keyword
geturl(keyword)

 邪惡一下,專門惡意點選競價排名。

相關文章