我正试图制作一本书,以输入Tik Tok账户的链接,每个账户在最近20个录像中分别有20个。 随附的法典是无所作为的。 任何帮助都得到真正的赞赏:
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
import json
#reload etc
def scroll_load(driver, scrolls=3):
for _ in range(scrolls):
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(2) # pause between scrolls
#scrape view count
def get_view_counts(driver, url):
driver.get(url)
time.sleep(4)
scroll_load(driver, scrolls=5)
view_counts_elements = driver.find_elements(By.XPATH, //*[@id="main-content-others_homepage"]/div/div[2]/div[2]/div/div[1]/div[1]/div/div/a/div/div[2]/strong )[:20] # update view-count-class
view_counts = [el.text for el in view_counts_elements]
return view_counts
def save_view_counts(urls, filename):
data = {}
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
for url in urls:
data[url] = get_view_counts(driver, url)
time.sleep(3) # delay between requests
driver.quit()
# save data
with open(filename, w ) as f:
f.write(json.dumps(data, indent=4))
# urls 2 scrape
urls = [
https://www.tiktok.com/@netflix ,
# ...
]
save_view_counts(urls, views.txt )
我预计,每个账户的数据将予以保存,指出每个账户的“URL”账户,并将计算在其最近的职位上。 然而,该产出只是一种ur,没有数据。