python实现提取百度搜索结果的方法

yipeiwu_com6年前Python基础

本文实例讲述了python实现提取百度搜索结果的方法。分享给大家供大家参考。具体实现方法如下:

# 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)']
def baidu_search(keyword,pn):
  p= {'wd': keyword} 
  res=urllib2.urlopen(("http://www.baidu.com/s?"+urllib.urlencode(p)+"&pn={0}&cl=3&rn=100").format(pn))
  html=res.read()
  return html
def getList(regex,text):
  arr = []
  res = re.findall(regex, text)
  if res:
    for r in res:
      arr.append(r)
  return arr
def getMatch(regex,text):
  res = re.findall(regex, text)
  if res:
    return res[0]
  return ""
def clearTag(text):
  p = re.compile(u'<[^>]+>')
  retval = p.sub("",text)
  return retval
def geturl(keyword):
  for page in range(10):
    pn=page*100+1
    html = baidu_search(keyword,pn)
    content = unicode(html, 'utf-8','ignore')
    arrList = getList(u"<table.*?class=\"result\".*?>.*?<\/a>", content)
    for item in arrList:
      regex = u"<h3.*?class=\"t\".*?><a.*?href=\"(.*?)\".*?>(.*?)<\/a>"
      link = getMatch(regex,item)
      url = link[0]
      #获取标题
      #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)
        uri=response.geturl()
        print uri
      except:
        continue
if __name__=='__main__':
  geturl('python')

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python 判断自定义对象类型

要判断自定义对象的类型,用__class__方法,或者用isinstance(object, class-or-type-or-tuple)-->bool 用__class__不能...

Django Rest framework三种分页方式详解

Django Rest framework三种分页方式详解

前言 我们数据库有几千万条数据,这些数据需要展示,我们不可能直接从数据库把数据全部读取出来. 因为这样会给内存造成巨大的压力,很容易就会内存溢出,所以我们希望一点一点的取. 同样,展示的...

python实现写数字文件名的递增保存文件方法

如下所示: col = [] img = "test1" img1 = "test2" col.append(img) col.append(img1) data=np....

对python3新增的byte类型详解

对python3新增的byte类型详解

在python2中字节类型同字符类型区分不大,但是在python3中最重要的特性是对文本和二进制数据做了更加清晰的区分,文本总是Unicode,由字符类型表示,而二进制数据则由byte类...

python列出目录下指定文件与子目录的方法

本文实例讲述了python列出目录下指定文件与子目录的方法。分享给大家供大家参考。具体实现方法如下: # if you know the exact name: import os...