Python五期爬虫作业

【Python爬虫作业】- 第11和12次,爬虫原理和网页构造,

2018-01-06  本文已影响17人  丽雁解

一:
简单叙述爬虫原理
答:从网页上请求数据,下载数据,解析数据,从而得到自己想要的内容。
二:
利用chrome浏览器查看某网站的源代码及审查元素
答:网页空白处,右键,选择“查看网页源代码”;按F12 审查元素。类似下图所示:


temp.png

三、定义一个类storeCsv
类的功能,对csv模块进行再次封装,要求:
1、判断您要创建的csv文件判断文件是否存在,存在则给予提示(可以是输出:文件已存在等语句)
2、将数据不换行写入csv文件
3、数据包含:姓名 年龄 城市 地址 职业 (数据自定义,至少写五行)
示例:class storeCsv():
def 函数():
代码
def 函数():
代码
....
test = storeCsv()
.....

import csv
import os

class storeCsv():
    name = ''
    age = 0
    city = ''
    address = ''
    job = ''

    def __init__(self, n, a, c, ad, j):
        self.name = n
        self.age = a
        self.city = c
        self.address = ad
        self.job = j

    def write(self, filename):
        if not os.path.isfile(filename):
            print('not a file!')
        else:
            item = [(self.name, self.age, self.city, self.address, self.job)]
            with open(filename,'a',encoding='utf8',newline='') as ff:
                ff_csv=csv.writer(ff)
                ff_csv.writerows(item)
                ff.close()
#怎样才能不换行呢?


f2 = 'test1.csv'
newcsv = storeCsv('king', 20, 'beijing', 'TY', 'engineer')
newcsv.write(f2)
newcsv2 = storeCsv('ding', 30, 'beijing', 'YT', 'engineer')
newcsv2.write(f2)
上一篇下一篇

猜你喜欢

热点阅读