python 爬虫百度搜索结果

因为合作的关系,用户在百度上搜索一些检索词时,百度会把我们网站放到搜索结果的第一位,但是经过实践发现,好多关键字都不是这样的。

所以写了一个小爬虫来检验2000-3000个关键字的移动和pc端百度搜索结果。

利用put的方法提交url,然后对返回的数据进行正则匹配,

找出第一位的搜索结果是不是含有我们网站的链接。

python 

# coding=utf-8
import urllib
import urllib2
from urllib2 import Request, urlopen, URLError, HTTPError
import re
import time
import random
# 将正则表达式编译成Pattern对象
#pattern = re.compile(r'(id="1"){1}.{2,30}(book.zongheng.com){1}.*\.html') #pc
pattern = re.compile(r'(>1.{0,6}<em>){1}.*(zongheng\.com){1}.*(2&?#?160)') #wap\h5
output = open('D:\\result.txt', 'a')
k=0
# 使用Pattern匹配文本,获得匹配结果,无法匹配时将返回None
#match = pattern.search('http://book.zongheng.com/book/262883.html?fr=p')
def search(key):
    global k
    url = 'http://m.baidu.com/s?word='+key  
      
    user_agent = 'Mozilla/4.1 (compatible; MSIE 5.5; Windows NT)'    
    values = {'wd' : '111' }    
      
    headers = { 'User-Agent' : user_agent }    

    data = urllib.urlencode(values) # 编码工作
    req = urllib2.Request(url)  # 发送请求同时传data表单
    #response = urllib2.urlopen(req)  #接受反馈的信息
    #the_page = response.read()  #读取反馈的内容
    #print the_page

    #req = Request('http://bbs.csdn.net/callmewhy') 
    try:  
      
        response = urllib2.urlopen(req) 
      
    except URLError, e:  

        if hasattr(e, 'code'):  
      
            print key+' The server couldn\'t fulfill the request.'  
      
            print 'Error code: ', e.code
            search(key)
        elif hasattr(e, 'reason'):  
      
            print key+' We failed to reach a server.'  
      
            print 'Reason: ', e.reason
            search(key)
      
      
    else:  
        #print 'No exception was raised.'  
        # everything is fine
        the_page = response.read()  #读取反馈的内容
        #print 'Real url :' + response.geturl()
        #print response.info()
        #print the_page
        k+=1
        if k%20==0:
            time.sleep(30)
        if '即可恢复使用' in the_page:
            print 'wait'
            time.sleep(30)
            print 'wait done'
            search(key)
        #return 1
        match = pattern.search(the_page)
        if match:
            pass
            # 使用Match获得分组信息
            #print key +" this key is ok"
            #print match.group()
        else:
            print key+" this key is not ok"
            output .write(key+"\n\t")
search('水系法师的春天')
file = open("D:\\test.txt")
while 1:
    lines = file.readlines(100000)
    if not lines:
        break
    for line in lines:
       time.sleep(2*random.random())
       search(line.strip())
       
output .close() 


Python爬虫百度搜索引擎是一种利用Python编程语言和爬虫技术来提取百度搜索内容的方法。通过编写Python代码,可以使用爬虫技术获取百度搜索结果页面的内容,并进一步进行内容分析和处理,从而简化搜索过程。具体的案例代码可以参考引用中提供的示例代码。 在这个示例代码中,通过使用Python的urllib库来发送HTTP请求,获取百度搜索结果页面的HTML源码。然后使用正则表达式对HTML源码进行解析,提取出搜索结果中的文字内容。如果搜索结果中没有相关的百度百科词条,代码会输出失败信息,并建议将搜索词条更具体化。 需要注意的是,在这个示例中还提到了两个微信公众号,分别是“因为Python”和“轻松学会Python爬虫”,可以关注这两个公众号获取更多相关的信息和教程。 总之,Python爬虫百度搜索引擎是一种利用Python编程语言和爬虫技术来简化百度搜索过程的方法,通过编写代码来提取和处理搜索结果的内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python爬虫爬取百度搜索内容代码实例](https://download.csdn.net/download/weixin_38635682/14842283)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Python爬虫——自制简单的搜索引擎](https://blog.csdn.net/jclian91/article/details/77387842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值