我的程序已经可以爬取网络内容,但不太清楚怎么保存到当地文件夹,用网上方法试着做,但是有报错
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 = '/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()
python爬取正确但不出文件_python爬取糗事百科 该如何正确保存到本地文件夹?报错-问答-阿里云开发者社区-阿里云...