同步操作将从 小柒2012商城/从零学Python 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# 导入requests库import requests# 导入文件操作库import codecsimport osfrom bs4 import BeautifulSoupimport sysimport importlibimportlib.reload(sys)# 给请求指定一个请求头来模拟chrome浏览器global headersheaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36'}server = 'http://www.xbiquge.la/'# 星辰变地址book = 'http://www.xbiquge.la/5/5623/'# 定义存储位置global save_pathsave_path = 'G:/星辰变'if os.path.exists(save_path) is False:os.makedirs(save_path)# 获取章节内容def get_contents(chapter):req = requests.get(url=chapter)html = req.contenthtml_doc = str(html, 'utf8')bf = BeautifulSoup(html_doc, 'html.parser')texts = bf.find_all('div', id="content")# 获取div标签id属性content的内容 \xa0 是不间断空白符 content = texts[0].text.replace('\xa0' * 4, '\n')return content# 写入文件def write_txt(chapter, content, code):with codecs.open(chapter, 'a', encoding=code)as f:f.write(content)# 主方法def main():res = requests.get(book, headers=headers)html = res.contenthtml_doc = str(html, 'utf8')# 使用自带的html.parser解析soup = BeautifulSoup(html_doc, 'html.parser')# 获取所有的章节a = soup.find('div', id='list').find_all('a')print('总章节数: %d ' % len(a))for each in a:try:chapter = server + each.get('href')content = get_contents(chapter)chapter = save_path + "/" + each.string.replace("?", "") + ".txt"write_txt(chapter, content, 'utf8')except Exception as e:print(e)if __name__ == '__main__':main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。