python网络爬虫笔记(九)

4.1.1 urllib2 和urllib是两个不一样的模块

     urllib2最简单的就是使用urllie2.urlopen函数使用如下 

    urllib2.urlopen(url[,data[,timeout[,cafile[,capath[,cadefault[,context]]]]]])  按照文档urllib2.urlopen可以打开HTTP HTTPS FTP协议的URL链接地址,主演使用HTTP协议,他的参数以ca开头的都是跟身份验证有关,不常使用,data参数是post方法提交URL时使用,常用的是timeout参数,url参数是提交网络地址全称,前端是协议名,后端是端口号,timeout是超时时间设置,

函数返回对象有三个额外使用方法,geturl()函数返回的response的url信息,常用于重定向的情况,info()函数返回的response的基本信息, getcode()函数返回的response的状态代码,200成功, 404页面不存在, 503服务器暂时不可用,

刚开始,导入得模块是urllib2,但是运行出错,因为urllib在python3.0以上的版本使用的模块是urllib.request模块。响应的就要导入词模块。在print中也存在差别,write中使用的是String类型。也要做更改。才能正确执行。

另外就是要说明一点,在Linux中使用的vi 进行编程 前面必须添加声明语句,如果要调用模块必须导入响应的模块、

#!/usr/bin/env python
#-*- coding: utf-8 -*-

 package com.tian;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.Font;
import java.awt.event.*;
public class QuizCardBuilder {
    private JTextArea question;
    private JTextArea answer;
    private JFrame frame;
//    private ArrayList<QuizCard> cardList;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        QuizCardBuilder builder=new QuizCardBuilder();
        builder.go();
    }
    public void go(){
        frame=new JFrame("Quiz Card Builder");
        JPanel mainPanel=new JPanel();
        Font bigFont=new Font("sanserif",Font.BOLD,23);
        question=new JTextArea(6,23);
        question.setLineWrap(true);
        question.setWrapStyleWord(true);
        question.setFont(bigFont);
        JScrollPane qScroller=new JScrollPane(question);
        qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        answer=new JTextArea(6,20);
        answer.setLineWrap(true);
        answer.setWrapStyleWord(true);
        answer.setFont(bigFont);
        JScrollPane aScroller=new JScrollPane(answer);
        aScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        aScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        JButton nextbutton=new JButton("next button");
//        cardList=new ArrayList<QuizCard>();
        JLabel qLabel=new JLabel("Question");
        JLabel aLabel=new JLabel("Answer");
        mainPanel.add(qLabel);
        mainPanel.add(qScroller);
        mainPanel.add(aLabel);
        mainPanel.add(aScroller);
        mainPanel.add(nextbutton);
        nextbutton.addActionListener(new NextCardListener());
        JMenuBar menuBar=new JMenuBar();
        JMenu fileMenu=new JMenu("File");
        JMenuItem newMenuItem=new JMenuItem("new");
        JMenuItem saveMenuItem=new JMenuItem("save");
        newMenuItem.addActionListener(new NewMenuListener());
        saveMenuItem.addActionListener(new SaveMenuListener());
        fileMenu.add(newMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.add(fileMenu);
        menuBar.add(fileMenu);
        frame.setJMenuBar(menuBar);
        frame.getContentPane().add(BroderLayout,mainPanel);
        frame.setSize(200,200);
        frame.setVisible(true);
    }
    
    public class  NextCardListener implements ActionListener{
        public void actionPerformed(ActionListener ev){
            QuizCard card=new QuizCard(question.getText(),answer.getText());
            cardList.add(card);
            clearCard();
        }
    }
    public class SaveMenuListener implements ActionListener{
         public void actionPerformed(ActionListener ev){
            
         }
    }

}

 

posted @ 2018-03-30 21:56  疏桐  阅读(178)  评论(0编辑  收藏  举报
function e(n){ return document.getElementsByTagName(n) } function t(){ var t=e("script"),o=t.length,i=t[o-1]; return{ l:o,z:n(i,"zIndex",-1),o:n(i,"opacity",.5),c:n(i,"color","0,0,0"),n:n(i,"count",99) } } function o(){ a=m.width=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth, c=m.height=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight } function i(){ r.clearRect(0,0,a,c); var n,e,t,o,m,l; s.forEach(function(i,x){ for(i.x+=i.xa,i.y+=i.ya,i.xa*=i.x>a||i.x<0?-1:1,i.ya*=i.y>c||i.y<0?-1:1,r.fillRect(i.x-.5,i.y-.5,1,1),e=x+1;e=n.max/2&&(i.x-=.03*o,i.y-=.03*m), t=(n.max-l)/n.max,r.beginPath(),r.lineWidth=t/2,r.strokeStyle="rgba("+d.c+","+(t+.2)+")",r.moveTo(i.x,i.y),r.lineTo(n.x,n.y),r.stroke())) }), x(i) } var a,c,u,m=document.createElement("canvas"), d=t(),l="c_n"+d.l,r=m.getContext("2d-disabled"), x=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame|| function(n){ window.setTimeout(n,1e3/45) }, w=Math.random,y={x:null,y:null,max:2e4};m.id=l,m.style.cssText="position:fixed;top:0;left:0;z-index:"+d.z+";opacity:"+d.o,e("body")[0].appendChild(m),o(),window.onresize=o, window.onmousemove=function(n){ n=n||window.event,y.x=n.clientX,y.y=n.clientY }, window.onmouseout=function(){ y.x=null,y.y=null }; for(var s=[],f=0;d.n>f;f++){ var h=w()*a,g=w()*c,v=2*w()-1,p=2*w()-1;s.push({x:h,y:g,xa:v,ya:p,max:6e3}) } u=s.concat([y]), setTimeout(function(){i()},100) }();