Python爬虫爬取百度搜索内容代码实例

作者:程序员的人生A 时间:2022-06-23 02:21:11 

这篇文章主要介绍了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="(.*?)" rel="external nofollow" rel="external nofollow" .*?>(.*?)</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')</a.*&#63;href="(.*?)" rel="external nofollow" rel="external nofollow" .*?></h3.*?class="t".*?></table.*?class="result".*?>

来源:https://www.cnblogs.com/chengxuyuanaa/p/13033108.html

标签:Python,爬虫,爬取,百度,内容
0
投稿

猜你喜欢

  • 虾米网今年三月所用加密解密函数

    2010-08-09 12:54:00
  • asp如何创建目录?

    2009-11-14 20:51:00
  • 网页设计之文字的辨识度与可读性

    2007-10-26 16:19:00
  • 教你轻松掌握MaxDB和MySQL之间的协同性

    2009-02-23 17:28:00
  • 怎样在GoDaddy-Linux主机上使用JSP连接到MySQL数据库

    2010-04-21 12:38:00
  • 功能强大,代码简单的管理菜单

    2008-07-11 16:52:00
  • IE9初窥:支持CSS3,和HTML5?

    2009-12-01 14:20:00
  • 教你为MySQL数据库换挡加速

    2010-03-03 16:58:00
  • 远古幻想ICON 1套+创作思路

    2007-09-30 20:33:00
  • 多级联动下拉选择框,动态获取下一级

    2008-09-04 10:34:00
  • 用IE浏览器UTF-8页面是一片空白

    2009-06-14 19:55:00
  • 不用加载Include文件也能生成选择列表吗?

    2009-10-29 12:30:00
  • go语言LeetCode题解720词典中最长的单词

    2023-08-05 19:46:04
  • Go语言转换所有字符串为大写或者小写的方法

    2023-06-21 19:48:07
  • pytorch 实现张量tensor,图片,CPU,GPU,数组等的转换

    2023-08-12 07:44:34
  • python命名关键字参数的作用详解

    2023-09-01 10:35:37
  • Go语言程序查看和诊断工具详解

    2023-06-22 02:40:38
  • Python爬虫信息输入及页面的切换方法

    2023-08-02 17:33:33
  • asp自动采集程序

    2009-02-04 10:11:00
  • sql函数:去掉html代码

    2008-04-07 12:44:00
  • asp之家 网络编程 m.aspxhome.com