关于Python爬虫无数据库的存储1之存储为csv格式

import urllib.request
from bs4 import BeautifulSoup
import csv
headers = ['部数','章节','链接',]
url = 'http://seputu.com/'

req = urllib.request.Request(url)
res = urllib.request.urlopen(req)
res.encoding = 'utf-8'
html = res.read().decode('utf-8')
soup = BeautifulSoup(html,'html.parser')
rows = []
for mulu in soup.find_all('div',class_='mulu'):
    h2 = mulu.find('h2')
    if h2!= None:
        h2_title = h2.string
        for a in soup.find(class_ = 'box').find_all('a'):
            href = a.get('href')
            title = a['title']
            #print(href,title)
            rows.append((h2_title,title,href))
print(rows)
with open(r'c:\Users\14547\Desktop\test\testCSV.csv','w') as f:
    f_csv = csv.writer(f)
    f_csv.writerow(headers)
    f_csv.writerows(rows)

这里有个坑,with open(r'c:\Users\14547\Desktop\test\testCSV.csv','w') as f:
这里直接复制的路径总是会报错,后来发现将C盘这里改为小写c就没问题了




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值