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

yipeiwu_com4年前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编程实现微信企业号文本消息推送功能示例

本文实例讲述了Python微信企业号文本消息推送功能。分享给大家供大家参考,具体如下: 企业号的创建、企业号应用的创建、组、tag、part就不赘述了,一搜一大堆,但是网上拿的那些个脚本...

Python读写/追加excel文件Demo分享

三个工具包 python操作excel的三个工具包如下,注意,只能操作.xls,不能操作.xlsx。 • xlrd: 对excel进行读相关操作 • xlwt:...

python解释器spython使用及原理解析

python解释器spython使用及原理解析

简介 出于个人爱好和某种需求,我再16年对python的解释器产生了浓厚兴趣,并且下定决心重新实现一个版本。我个人再游戏服务器开发中,对c++嵌入lua和python都有着丰富应用经验...

Pandas0.25来了千万别错过这10大好用的新功能

Pandas0.25来了千万别错过这10大好用的新功能

呆鸟云:“7 月 18 日,Pandas 团队推出了 Pandas 0.25 版,这就相当于 Python 3.8 啦,Python 数据分析师可别错过新版的好功能哦。” 安装 0.2...

python批量提取word内信息

单位收集了很多word格式的调查表,领导需要收集表单里的信息,我就把所有调查表放一个文件里,写了个python小程序把所需的信息打印出来 #coding:utf-8 impo...