zoukankan      html  css  js  c++  java
  • 2017年6月28日 python爬虫学习

    1.写入csv文件
    2.lxml的用法
    3.自定义字典类的方法
    4.bytes解码得到str,str编码得到bytes
    5.json

    1

    import csv
    import lxml.html
    class ScrapcCallback:
        def __init__(self):
            self.witer=csv.writer(open("test.csv","a"))
            self.fields =("name","age","gred")
            self.witer.writerow(self.fields)

    2

    tree= lxml.html.fromstring(html)
    tree.cssselect('#content > p ')
    

    3.

    class my_dict:
    
        def __setitem__(self, folder, result):
    
            print(bool(folder))
            if not  folder:
    
                os.mkdir(folder)
            with open("test","wb") as fp:
                fp.write(pickle.dumps(result))
        def __getitem__(self, item):
            if os.path.exists(item):
                with open(item,"rb") as fp:
                    return  pickle.load(fp)
    
    #先实例化在使用
    

    4.

    s = b"hello world"
    print(s, type(s))
    # 解码
    s_1 = s.decode('utf-8')
    print(s_1, type(s_1))
    # 编码
    s_2 = s_1.encode("utf-8")
    print(s_2, type(s_2))

    这里我想吐槽下,python竟然不认识这样的.....

    string="{'name':'tom'}"

    只认这样的.....................oh,god.......

    string = '{"name":"tom"}'

    哎 ,,,心累啊 

  • 相关阅读:
    FileDescriptor详解
    java序列化
    ObjectInputStream和ObejctOutputStream
    ByteArrayOutputStream
    ByteArrayInputStream
    PipedInputStream/PipedOutputStream
    字节输入流
    反义
    贪婪和非贪婪
    MYSQL数据库优化
  • 原文地址:https://www.cnblogs.com/twotigers/p/7090896.html
Copyright © 2011-2022 走看看