【爬虫】爬取百度搜索结果页面

今日看了一下爬虫,写了一个爬取百度搜索页面的小程序。可以在代码中改动搜索词,代码如下:

#coding=utf-8
#python version:2.7
#author:sharpdeep

import urllib
import urllib2
import re
from bs4 import BeautifulSoup as BS

baseUrl = 'http://www.baidu.com/s'
page = 1 #第几页
word = '穿戴设备'  #搜索关键词

data = {'wd':word,'pn':str(page-1)+'0','tn':'baidurt','ie':'utf-8','bsst':'1'}
data = urllib.urlencode(data)
url = baseUrl+'?'+data

try:
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
except urllib2.HttpError,e:
    print e.code
    exit(0)
except urllib2.URLError,e:
    print e.reason
    exit(0)

html = response.read()
soup = BS(html)
td = soup.find_all(class_='f')

for t in td:
    print t.h3.a.get_text()
    print t.h3.a['href']

    font_str = t.find_all('font',attrs={'size':'-1'})[0].get_text()
    start = 0 #起始
    realtime = t.find_all('div',attrs={'class':'realtime'})
    if realtime:
        realtime_str = realtime[0].get_text()
        start = len(realtime_str)
        print realtime_str
    end = font_str.find('...')
    print font_str[start:end+3],'\n'

https://github.com/sharpdeep/CrawlerBaidu

转载于:https://www.cnblogs.com/sharpdeep/p/10629551.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值