我的程序已经可以爬取网络内容,但不太清楚怎么保存到当地文件夹,用网上方法试着做,但是有报错
import urllib
import urllib.request
import requests ##导入requests
from bs4 import BeautifulSoup ##导入bs4中的BeautifulSoup
import os
for i in range(35):
page=i
headers = {'User-Agent':"Mozilla/5.0 (Windows NT 6.1; WOW64)"}
all_url = 'http://www.qiushibaike.com/hot/page/'+str(page) ##开始的URL地址
start_html = requests.get(all_url,headers=headers)
Soup = BeautifulSoup(start_html.text, 'lxml')
all_a = Soup.find('div', class_='col1').find_all(class_='content')
for j in all_a:
title = j.get_text() #取出a标签的文本
path = str(title).strip()
os.makedirs(os.path.join("C:\\Users\\DOU XIAOFEI\\Desktop\\wintercode\\funstory", path))
os.chdir("C:\\Users\\DOU XIAOFEI\\Desktop\\wintercode\\funstory\\"+path) ##切换到上面创建的文件夹
name = all_url[-9:-4]
allurl = request.get(all_url,headers=headers)
f = open(name + '.html', 'ab')
f.write(allurl.content)
f.close()
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
报错发出来去掉路径中的空格试试i