def read(self): self.words=[] if os.path.exists("words.txt"): try: f=open("words.txt","rt") while True: s=f.readline().strip("\n") if s=="": break s=s.split(",") if len(s)==2: self.words.append(Word(s[0],s[1])) f.close() except Exception as e: print(e)这个函数的功能
时间: 2023-11-15 12:04:29 浏览: 37
这个函数是一个类的方法,它的功能是从本地文件 "words.txt" 中读取数据,并将读取的数据存储在类的属性 self.words 中。具体来说,这个函数做了以下几件事情:
1. 初始化类的属性 self.words 为空列表。
2. 判断本地文件 "words.txt" 是否存在,如果存在则打开文件并读取其中的数据。
3. 读取文件的每一行,去掉行末的换行符,并将其按逗号分割成两个字符串。
4. 如果分割后得到的列表长度为 2,则将其作为参数创建一个 Word 类的对象,并将其添加到 self.words 列表中。
5. 关闭文件。
需要注意的是,这个函数可能会抛出异常,例如文件不存在或者读取文件时出现错误,这时候它会将异常信息打印出来。
相关问题
def __exists(self,no): #判断学号是否存在 for stu in self.stulist: if stu.no == no: return True else: return False解释代码
这段代码是一个名为 `__exists` 的私有方法,用于判断给定的学生学号是否存在于学生列表 `self.stulist` 中。
方法中使用了一个 `for` 循环,遍历学生列表中的每个学生对象。如果某个学生对象的学号与方法参数 `no` 相等,则返回 `True`,表示该学号存在于学生列表中;否则方法会继续遍历下一个学生对象。如果遍历完所有学生对象后仍然没有找到与 `no` 相等的学号,则返回 `False`,表示该学号不存在于学生列表中。
这里使用了 Python 中的 `else` 子句,它与 `for` 循环配合使用,表示当循环正常结束时(即没有执行 `break` 语句),执行 `else` 子句中的语句。因此,在这里 `else` 子句中的 `return False` 语句表示没有找到与 `no` 相等的学号,因此返回 `False`。
from requests_html import HTMLSession import os class Spider: def init(self): self.base_url = 'https://s3-ap-northeast-1.amazonaws.com/data.binance.vision/data/spot/daily/klines' self.pair = '1INCHBTC' self.interval = '1d' self.session = HTMLSession() def get_urls(self): urls = [] # 首页 response = self.session.get(f'{self.base_url}/{self.pair}/{self.interval}/') if response.status_code == 200: for link in response.html.links: if link.endswith('.zip'): urls.append(link) # 分页 while True: response = self.session.get(response.html.links[-1]) if response.status_code != 200: # 请求失败 break for link in response.html.links: if link.endswith('.zip'): urls.append(link) if 'CHECKSUM' in response.html.links[-1]: break return urls def download_files(self): urls = self.get_urls() if not urls: print('下载失败') return if not os.path.exists('download_files'): os.mkdir('download_files') for url in urls: file_name = url.split('/')[-1] file_path = f'/Users/huyang/Desktop/2023/Grach/{file_name}' if os.path.exists(file_path): # 文件已存在 print(f'{file_name} 已存在') continue response = self.session.get(url) if response.status_code != 200: # 请求失败 print(f'{file_name} 下载失败') continue with open(file_path, 'wb') as f: f.write(response.content) print(f'{file_name} 下载成功') def run(self): self.download_files(),将对应的下载数据的网站地址改为'http://www.greenfinance.org.cn/'
您需要更改 Spider 类中的以下属性和方法:
```python
class Spider:
def __init__(self):
self.base_url = 'http://www.greenfinance.org.cn/'
self.session = HTMLSession()
def get_urls(self):
urls = []
response = self.session.get(self.base_url)
if response.status_code == 200:
for link in response.html.links:
if link.endswith('.xls') or link.endswith('.xlsx'):
urls.append(link)
return urls
def download_files(self):
urls = self.get_urls()
if not urls:
print('下载失败')
return
if not os.path.exists('download_files'):
os.mkdir('download_files')
for url in urls:
file_name = url.split('/')[-1]
file_path = f'/Users/huyang/Desktop/2023/Grach/{file_name}'
if os.path.exists(file_path):
print(f'{file_name} 已存在')
continue
response = self.session.get(url)
if response.status_code != 200:
print(f'{file_name} 下载失败')
continue
with open(file_path, 'wb') as f:
f.write(response.content)
print(f'{file_name} 下载成功')
def run(self):
self.download_files()
```
这里的 `get_urls()` 方法会获取 http://www.greenfinance.org.cn/ 页面中所有以 `.xls` 或 `.xlsx` 结尾的链接,而 `download_files()` 方法会下载这些链接指向的文件。请注意更改 `file_path` 变量以指向您想要保存文件的位置。最后,您可以通过调用 `run()` 方法来运行该类并下载文件。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)