from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from threading import Thread
browser = webdriver.Chrome()
url = input("Enter an URL >>>")
FileName = input("What to save as >>>")
tweets_list = []
browser.get(url)
sleep(1)
body = browser.find_element_by_tag_name("body")
def saver():
sleep(2)
global tweets_list
with open(FileName,"w+") as f:
for i in tweets_list:
try:
f.write(i.replace("\n",",")+"\n")
print(i)
except:
print("ERROR"*99)
T1 = Thread(target=saver,daemon=True)
T1.start()
while True:
for _ in range(500):
body.send_keys(Keys.PAGE_DOWN)
tweets = browser.find_elements_by_class_name("tweet-text")
for tweet in tweets:
if tweet.text.replace("\n\n","\n") in tweets_list:
pass
elif tweet.text.replace("\n\n","\n") not in tweets_list:
tweets_list.append(tweet.text.replace("\n\n","\n"))
我试图从childHello()调用parentHello()。我想出的唯一方法是将结构更改为圆形,如下所示:
class Test {
constructor() {
this.childObject = new Child();
}
parentHello() {
console.log("Parent Hello World!");
}
}
class Child {
childHello() {
// How do I call parentHello() from here?
}
}
const obj = new Test();
obj.childObject.childHello();
但在这样做之后,我再也无法将其转换为JSON了。有没有正确的方法来做到这一点?
编辑:我也尝试过super()但它只在扩展时才有效。答案 0 :(得分:1)
我认为您通过构造函数引用父级的解决方案是可以的。要允许序列化为JSON,您可以使用JSON.stringify
(替换器)的第二个参数:
一个改变字符串化过程行为的函数,或一个String和Number对象数组,用作白名单,用于选择/过滤要包含在JSON字符串中的值对象的属性。如果此值为null或未提供,则对象的所有属性都包含在生成的JSON字符串中。
class Test {
constructor() {
this.childObject = new Child(this);
}
parentHello() {
console.log("Parent Hello World!");
}
}
class Child {
constructor(parent) {
this.parent = parent;
}
childHello() {
this.parent.parentHello();
}
}
const obj = new Test();
var cache = [];
var json = JSON.stringify(obj, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache.includes(value)) {
return;
}
cache.push(value);
}
return value;
});
console.log(json);