|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import requests
- from bs4 import BeautifulSoup
- import re
- url = 'https://www.biduo.cc/biquge/41_41864/c15143933.html'
- headers = {
- 'Accept-Language': 'zh-CN',
- 'Cache-Control': 'no-cache',
- 'Connection': 'Keep-Alive',
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18363'
- }
- html = requests.get(url=url,headers=headers).text
- soup = BeautifulSoup(html,'lxml')
- title = soup.select('.bookname > h1')
- title = title[0].text
- text = soup.find('div',id='content').text.replace(' ','\n')
- with open('无敌1.txt','w',encoding='utf-8') as f:
- while (len(text)>50):
- str1 = text[0:50]
- if ('\n') in str1:
- index = str1.find('\n')
- str1 = str1[0:index+1]
- text = text[index+1:]
- f.write(str1)
- else:
- if (text[50]) is '\n':
- f.write(str1)
- text = text[50:]
-
- else:
- f.write(str1)
- f.write('\n')
- text = text[50:]
- else:
- f.write(text)
- print('完成爬取')
复制代码
有没有大佬有爬完文本后向文件里写的时候控制每行字符长度的程序。我自己写了一个,感觉不是很理想。求大佬指点 |
|