python网页解析库_Python实现抓取网页并且解析的实例

本文以实例形式讲述了Python实现抓取网页并解析的功能。主要解析问答与百度的首页。分享给大家供大家参考之用。

主要功能代码如下:

#!/usr/bin/python

#coding=utf-8

import sys

import re

import urllib2

from urllib import urlencode

from urllib import quote

import time

maxline = 2000

wenda = re.compile("href=\"http://wenda.so.com/q/.+\?src=(.+?)\"")

baidu = re.compile("更多知道相关问题.*?")

f1 = open("baidupage.txt","w")

f2 = open("wendapage.txt","w")

for line in sys.stdin:

if maxline == 0:

break

query = line.strip();

time.sleep(1);

recall_url = "http://www.so.com/s?&q=" + query;

response = urllib2.urlopen(recall_url);

html = response.read();

f1.write(html)

m = wenda.search(html);

if m:

if m.group(1) == "110":

print query + "\twenda\t0";

else:

print query + "\twenda\t1";

else:

print query + "\twenda\t0";

recall_url = "http://www.baidu.com/s?wd=" + query +"&ie=utf-8";

response = urllib2.urlopen(recall_url);

html = response.read();

f2.write(html)

m = baidu.search(html);

if m:

print query + "\tbaidu\t1";

else:

print query + "\tbaidu\t0";

maxline = maxline - 1;

f1.close()

f2.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值