zoukankan      html  css  js  c++  java
  • 爬取标题和价格

    coding:utf-8
    import re
    import urllib

    url = "http://www.muyingzhijia.com/Shopping/subcategory.aspx?cateID=31"


    def getHtml(url):
        page = urllib.urlopen(url)
        html = page.read()
        return html


    def getDSJ(html):

        reg = r'a href="(.*?)" class="traceCount landingStats" target="_blank" title="(.*?)"'    target位置不能变且有一个空格的距离
        lsre = re.compile(reg)
        ls = re.finditer(lsre, html)

        fd = open('test.txt', 'a')
        for imgurl in ls:

            fd.write(imgurl.group(2) + ' ' + imgurl.group(1) + ' ')
        fd.close()

    html = getHtml(url)

    ls = getDSJ(html)
    print 'ok'


    对文件的读写操作
    fo = open('/root/test.txt') #打开文件
    fo.read() #读取文件
    fo.close() #关闭文件  
    f1 = file('/root/test.txt') #表示类
    f1.close()  #一定要记得关闭文件
    cat .txt   读取txt的文件
    fnew = open('/root/new.txt','w')
    fnew.write('hello  i am milo')
    fnew.close() #必须关闭之后才会在文件中显示
    fnew = open('/root/new.txt','r+')
    fnew.read()
    fnew.write("new con")
    fenw.close()  #先读再写,新加的文字会在文件后边
    fnew = open('/root/new.txt','r+')
    fnew.write("nooooo")
    fnew.close()  #直接写会出现在文件开头
    r 只读    r+读写        w写入,先删除原文件,再重新写入,如果原文件没有则创建
    w+ 读写,先删除原文件,再重新写入,如果原文件没有则创建(可以写入输出)
    a  写入:在文件末尾追加新内容,文件不存在,创建之
    a+  读写:在文件末尾追加新内容,文件不存在,创建之
    b  打开二进制的文件 可以与r,w,a,+结合使用
    U支持所有的换行符号  " "" "" "

    f.readline()#一行一行的读取文件,超出后显示空字符串
    f.readlines()#返回一个列表
    f.next()#一行一行的读取,超出后停止迭代
    f.writelines()#写入多行数据
    f.seek(0,0)#指针在文件开头         选项=0表示文件指针指向从文件头部到“偏移量”字节处
    f.seek(偏移量,选项)                选项=1表示指文件指针指向从文件的当前位置,向后移动“偏移量”的字节
                                          选项=2表示将文件指针指向文件的尾部,向前移动“偏移量”字节

    偏移量有正负之分,正数向右,负数向左
    f.fluse()#提交更新,不用f.close()就可以直接查看新写入的文件
    文件查找 
    cat a.t
    hello word
    hello hello word  统计文件中hello的个数
    import re
    fp = file("a.t","r")
    count = 0
    for s in fp.readlines():
        li = re.findall("hello",s)
        li len(li)>o:
                 count = count+len(li)
    print "Search"+count+“hello”
    fp.close()

    把a.t中的hello替换成csvt,并保存到文件a2.t
    fp1 = file("a.t","r")
    fp2 = file("a2.t","w")
    for s in f1.readlines():
          fp2.write(s.replace("hello","cvst"))
    fp1.close()
    fp2.close()
    把a.t中的hello替换成csvt
    fp1 = file("a.t","r+")
    s = f1.read():
    f1.seek(0,0)
    f1.write(s.replace("hello","csvt"))  hello比csvt多一个字母,替换时正好覆盖掉,若hel替换csvt则不能完全覆盖
    fp1.close()

    os模块

  • 相关阅读:
    SpringMVC 使用JSR-303进行校验 @Valid
    Hibernate Tools生成注释
    大型网站架构演变和知识体系(转载)
    eclipse从数据库逆向生成Hibernate实体类
    性能测试公众号,欢迎你的加入~
    mysql使用druid监控配置
    (转)面试为什么需要了解JVM
    (转)什么是缓存击穿?
    Mysql推荐使用规范(转)
    java应用监控工具
  • 原文地址:https://www.cnblogs.com/ZHANG576433951/p/6086709.html
Copyright © 2011-2022 走看看