python 百度cpc点击

# 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)

 邪恶一下,专门恶意点击竞价排名。

posted @ 2016-06-27 21:48  BigRain  阅读(643)  评论(0编辑  收藏  举报