python实现提取百度搜索结果的方法
作者:MXi4oyu 时间:2021-05-13 20:27:21
本文实例讲述了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,百度
0
投稿
猜你喜欢
详解Python单元测试的两种写法
2023-02-13 06:04:20
Python处理mat文件的三种方式小结
2022-01-21 23:04:30
php在linux环境中如何使用redis详解
2023-06-14 23:38:30
SQL中位数函数实例
2024-01-25 17:21:55
Windows Server 2019 MySQL数据库的安装与配置理论+远程连接篇
2024-01-18 21:19:56
MySQL日志维护策略汇总
2024-01-26 18:11:53
django使用django-apscheduler 实现定时任务的例子
2021-06-09 05:10:44
MySQL数据库的触发器的使用
2024-01-19 07:17:37
perl控制流介绍(if条件,while,for循环,foreach)
2023-02-13 16:33:34
原生js实现查找/添加/删除/指定元素的class
2024-04-18 09:44:46
如何正确的解决 MySQL中忽略用户的现象
2008-11-27 16:00:00
vue调用高德地图实例代码
2024-06-07 15:20:47
JS 中如何判断 null
2007-12-13 20:28:00
Python 类的继承实例详解
2021-04-30 15:54:09
Go语言切片前或中间插入项与内置copy()函数详解
2024-05-22 10:16:19
Python psutil模块简单使用实例
2023-08-10 21:15:37
pytorch 常用函数 max ,eq说明
2023-06-25 11:09:21
Python数据分析之缺失值检测与处理详解
2021-10-04 01:09:08
Python利用matplotlib.pyplot.boxplot()绘制箱型图实例代码
2022-11-18 05:08:26
PHP实现手机归属地查询API接口实现代码
2023-11-14 09:10:33