# coding=utf8import Urllib2import stringimport urllibimport reimport random# set multiple user_agents to prevent Baidu limit ipuser_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= ' Fuzhou, Xiamen, Shenzhen, Guangzhou, Zhuhai, Foshan, Dongguan, Nanchang, Jiujiang, Shanghai, Hangzhou, Wenzhou, Ningbo, Shijiazhuang, Beijing, Baoding, Kunming, Chengdu, Nanjing, ". Split (', ') Keywrods_ category= "Male MedicalHospital, Male hospital, men's Disease hospital, Men's hospital, wrapping too long, wrapping surgery, wrapping surgery costs, erection bad, premature ejaculation, male, premature ejaculation surgery, impotence, premature ejaculation hospital, premature ejaculation costs, prostate, prostate hospital, enlarged prostate, infertility, sterility. 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.headersres.close () #print "%s\r\n=====html finished ======\r\n"% Htmlreturn htmldef GETCPC (regex,text): arr = []res = Re.findall (regex, text) for R ' Res:if r not ' in Arr:arr.append (r) retur n arrdef 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 = Itemprint "\r\n# math url:\r\n%s \ r \ n"%ur L# gets the title #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 10suri=response.geturl () response.close () print "*target URL: \ r \ n%s \ r \%uriexcept:continueif __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"%keywordgeturl (keyword)
Evil a bit, specifically malicious click on the bid rankings.
Python Baidu CPC click