zoukankan      html  css  js  c++  java
  • Python ===if while for语句 以及一个小小网络爬虫实例

    if分支语句

    >>> count=89

    >>> if count==89:

           print count

    89                          #单分支

    >>> 

    #coding:utf-8

    count=int(raw_input('请输入一个数字'))

    print count

    if count>80:

        print '比80大'

    else:

         if count<80:

           print ‘比80小’   #多分支

    =======自定义函数  while if else===========

    #coding:utf-8

    count=int(raw_input('请输入一个数字'))

    print count

    if count>80:

        print '比80大'

    else:

        print '比80小'

    print 'End'

    sex=raw_input('请输入您的性别')

    def inputsex(sex):

        while(sex!='male' and sex!='female'):

            print sex

            sex=raw_input('请输入性别为male 或者 female')

        if sex=='male':

            print 'Gentleman!'

        else:

            if sex=='female':

                print 'Lady'

           

    inputsex(sex)

    =====if else的关系表达式bool判断 非0即真!=====

    #coding:utf-8

    if True:

        print '1True'

    else:

        print 'False'

    if 0:

        print '2True'

    else:

        print '2False'

    if 1:

        print '3True'

    else:

        print '3False'

    if 298:

        print '4True'

    else:

        print '4False'

    if -2:

        print '5True'

    else:

    print '5False'

     1True

    2False

    3True

    4True

    5True

    If

    if (A and B):

    if (A or B):

    if not A:

    =======================while循环体========================

    ========网络刷浏览量的爬虫=======

    #coding:utf-8

    import time

    import webbrowser

    import os

    import random

    count=random.ranint(2,8)

    i=1

    j=0

    while j<count:

        while i<=3:

            webbrowser.open_new_tab('www.baidu.com')

            i=i+1

            time.sleep(3)

        else:

            os.system('taskkill /F /IM iexplore.exe')

        j=j+1    

    #windows下用taskkill  用的时候上网搜一下

    #linux系统下用kill -pid  或者 killall chrome

    #ranint就是随机整数

    =======for循环语句============

    for val in sth.  其中val不用预先声明

    For遍历字符串

    #coding:utf-8

    s1='www.baidu.com'

    i=0

    for n in s1:

        print format(i,'2d'),n             

        i=i+1

    else:

    print 'out for'

    #format(i,’2d’)使i占两个输出位

    For遍历list列表数组

    #coding:utf-8

    list1=[0,11,45,'dkfjah',12.5]              列表类型

    i=0

    for val in list1:

        print format(i,'2d'),val

    i=i+1

    也可以直接写成这样

    #coding:utf-8

    i=1

    for val in [11,23,0,'dfadf','国语',12.45]:

        print format(i,'2d'),val

        i=i+1

    将字符串转换成list   list(str)

    #coding:utf-8

    s1='www.baidu.com'

    i=1

    list1=list(s1)

    print list1

    for val in list1:

        print format(i,'2d'),val

        i=i+1

    元组 for遍历元组tuple

    #coding:utf-8

    #用圆括号括起来的是元组,元组中的数据只可读,不可修改。

    tup=(1,2,3,4,5)  元组类型

    for t in tup:

        print t

    else:

    print 'out tup'

    for遍历文件  for val in file.readlines()

    #coding:utf-8

    #如果所读的文件与此py程序文件所在地址一样,则直接写文件名

    for s in open('11.txt','r').readline():

        print s

    li3=open('11.txt','r').readlines()

    for a in open('11.txt','r').readlines():

        open('tmp.txt','a+').write(a)  #a+是追加写入  r 读 w写 w+如果没有此文件先创建再写入

        print a

    print len(li3)

    #len(li3)输出列表有多长  这里即文章有多少行

    #readline()返回字符串  默认返回第一行

    #readlines()返回list 默认为文件中所有行的list

    #用help(file.readline)查看帮助

    Python 爬虫如何获取 JS 生成的 URL 和网页内容?

    我是直接看js源码,分析完,然后爬的。
    例如看页面是用Ajax请求一个JSON文件,我就先爬那个页面,获取Ajax所需的参数,然后直接请求JSON页,然后解码,再处理数据并入库。
    如果你直接运行页面上所有js(就像浏览器做的那样),然后获取最终的HTML DOM树,这样的性能非常地糟糕,不建议使用这样的方法。因为Python和js性能本身都很差,如果这样做,会消耗大量CPU资源并且最终只能获得极低的抓取效率。
  • 相关阅读:
    flutter 和 NTFS
    APIO2020 游记
    CF1336F Journey
    ZJOI2020 游记
    CF568E Longest Increasing Subsequence
    CSP2020 游记
    洛谷 P6217 简单数论题
    CF587F Duff is Mad
    CF526G Spiders Evil Plan
    WC2021 游记
  • 原文地址:https://www.cnblogs.com/AmilyWilly/p/5103488.html
Copyright © 2011-2022 走看看