python 小说下载_通过python自动获取小说并下载

1 importurllib.request2 importos3

4 headers ={5 "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163"

6 "Safari/535.1",7 }8

9

10 #主程序,获取每个章节的名称及链接

11 defmain():12 print('开始下载')13 url_list =[]14 url = 'http://www.eywedu.com/honglou/01/index.htm'

15 res = urllib.request.Request(url, data=None, headers=headers)16 html = urllib.request.urlopen(res).read().decode('gb18030')17 lists = html.split('')34 get_content(net+cut[0], cut[1])35 print('第', page, '章已下完')36 page = page+1

37

38

39 #根据每章链接获得内容

40 defget_content(url, name):41 paragraphs =[]42 res = urllib.request.Request(url, data=None, headers=headers)43 html = urllib.request.urlopen(res).read().decode('gb18030')44 lists = html.split('
')45 t =046 while t

')[1]49 p = p.replace('\r\n', '')50 paragraphs.append(p)51 elif t == len(lists) - 1:52 p = lists[t].split(r'')[0]53 p = p.replace('\r\n', '')54 paragraphs.append(p)55 else:56 p = lists[t].replace('\r\n', '')57 paragraphs.append(p)58 t = t + 1

59 save_txt(paragraphs, name)60

61

62 #将得到的信息保存

63 defsave_txt(content, name):64 f = open('红楼梦\\'+name+'.txt', "a", encoding='utf-8')65 for i incontent:66 f.write(i)67 f.close()68

69

70 if __name__ == '__main__':71 main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现自动获取目标网站最新通知,可以使用 Python 中的 requests 和 BeautifulSoup 库来爬取网页信息,具体步骤如下: 1. 发送请求获取网页内容 使用 requests 库发送 GET 请求获取目标网站的 HTML 页面内容,代码如下: ```python import requests url = '目标网站 URL' response = requests.get(url) html = response.text ``` 2. 解析 HTML 页面内容 使用 BeautifulSoup 库解析 HTML 页面内容,并提取出需要的信息,例如网站最新通知的标题和链接,代码如下: ```python from bs4 import BeautifulSoup soup = BeautifulSoup(html, 'html.parser') # 提取最新通知的标题和链接 title = soup.find('a', attrs={'class': 'latest-title'}).text link = soup.find('a', attrs={'class': 'latest-title'}).get('href') ``` 3. 发送通知 最后可以使用第三方库(如 win10toast)发送通知,提醒用户网站有新的通知。代码如下: ```python from win10toast import ToastNotifier toaster = ToastNotifier() toaster.show_toast(title, link, duration=10) ``` 综合起来,实现自动获取目标网站最新通知的完整代码如下: ```python import requests from bs4 import BeautifulSoup from win10toast import ToastNotifier url = '目标网站 URL' response = requests.get(url) html = response.text soup = BeautifulSoup(html, 'html.parser') title = soup.find('a', attrs={'class': 'latest-title'}).text link = soup.find('a', attrs={'class': 'latest-title'}).get('href') toaster = ToastNotifier() toaster.show_toast(title, link, duration=10) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值