惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
雷峰网
雷峰网
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
G
Google Developers Blog
腾讯CDC
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
博客园_首页
T
Tailwind CSS Blog
C
Check Point Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
Y
Y Combinator Blog
L
LangChain Blog
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI

博客园 - BigRain

小学生学习汉字,汉字抓取 c# regex regextools cache 访问频率的思考 20130118SQL记录 PPT辅助工具 检查外链的方法 webService启用cookie的另一种方法 百度调价HttpWebRequest 装饰者模式 第一个android程序闪亮登场 SQL 的一些批量插入和修改 职责链模式的运用 State Pattern 程序人生 SerialPort实现modem的来电显示 利用枚举进行状态的设计 singleton模式 在软件开发中的运用 我对当前项目的一些看法 闲话闲说——关于异常
python 百度cpc点击
BigRain · 2016-06-27 · via 博客园 - BigRain
# 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)