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("<a href=\"http://www.baidu.com/link\?url=.+\".*?>更多知道相关问题.*?</a>")
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()

希望本文所述对大家Python程序设计的学习有所帮助。


谁用过python中的re来抓取网页,可以否给个例子,

这是我写的一个非常简单的抓取页面的脚本,作用为获得指定URL的所有链接地址并获取所有链接的标题。

===========geturls.py================
#coding:utf-8
import urllib
import urlparse
import re
import socket
import threading

#定义链接正则
urlre = re.compile(r"href=[\"']?([^ >\"']+)")
titlere = re.compile(r"<title>(.*?)</title>",re.I)

#设置超时时间为10秒
timeout = 10
socket.setdefaulttimeout(timeout)

#定义最高线程数
max = 10
#定义当前线程数
current = 0

def gettitle(url):
global current
try:
content = urllib.urlopen(url).read()
except:
current -= 1
return
if titlere.search(content):
title = titlere.search(content).group(1)
try:
title = title.decode('gbk').encode('utf-8')
except:
title = title
else:
title = "无标题"
print "%s: %s" % (url,title)
current -= 1
return

def geturls(url):
global current,max
ts = []
content = urllib.urlopen(url)
#使用set去重
result = set()
for eachline in content:
if urlre.findall(eachline):
temp = urlre.findall(eachline)
for x in temp:
#如果为站内链接,前面加上url
if not x.startswith("http:"):
x = urlparse.urljoin(url,x)
#不记录js和css文件
if not x.endswith(".js") and not x.endswith(".css"):
result.add(x)
threads = []
for url in result:
t ......余下全文>>
 

怎使用python抓取网页并实现一些提交操作?

下面这个程序是抓取网页的一个例子,MyOpener类是为了模拟浏览器客户端,并采用随机选取的方式以防网站将你认为是机器人。
MyFunc函数抓取你指定的url,并提取了其中的href链接,图片的获取类似,一般是<img src=xxx>这样的形式,其他的功能应该也不难,去网上搜下应该有些例子。

import re
from urllib import FancyURLopener
from random import choice

user_agents = [
'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
'Opera/9.25 (Windows NT 5.1; U; en)',
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9'
]

class MyOpener(FancyURLopener, object):
version = choice(user_agents)

def MyFunc(url):
myopener = MyOpener()
s = myopener.open(url).read()
ss=s.replace("\n"," ")
urls=re.findall(r"<a.*?href=.*?<\/a>",ss,re.I)#寻找href链接
for i in urls:
do sth.
 

评论关闭