Ich musste zu einem Headless-Browser wechseln, weil ich leere Tabellen durchsucht habe, um herauszufinden, wie das HTML der Website funktioniert, und ich gebe zu, dass ich nicht verstehe, wie es funktioniert.
Ich möchte es auch Vervollständigen Sie die Links, damit sie für die weitere Verwendung funktionieren. Dies sind die letzten drei Zeilen des folgenden Codes:
Code: Select all
from playwright.sync_api import sync_playwright
# headless browser to scrape
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://fbref.com/en/comps/9/Premier-League-Stats")
#open the file up
with open("path", 'r') as f:
file = f.read()
years = list(range(2024,2022, -1))
all_matches = []
standings_url = "https://fbref.com/en/comps/9/Premier-League-Stats"
for year in years:
standings_table = page.locator("table.stats_table").first
link_locators = standings_table.get_by_role("link").all()
for l in link_locators:
l.get_attribute("href")
print(link_locators)
link_locators = [l for l in links if "/squads/" in l]
team_urls = [f"https://fbref.com{l}" for l in link_locators]
print(team_urls)
browser.close()
Code: Select all
Traceback (most recent call last):
File "path", line 27, in
link_locators = standings_table.get_by_role("link").all()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "path\.venv\Lib\site-packages\playwright\sync_api\_generated.py", line 15936, in all
return mapping.from_impl_list(self._sync(self._impl_obj.all()))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "path\.venv\Lib\site-packages\playwright\_impl\_sync_base.py", line 102, in _sync
raise Error("Event loop is closed! Is Playwright already stopped?")
playwright._impl._errors.Error: Event loop is closed! Is Playwright already stopped?
Process finished with exit code 1
Ich kann die href-Links einfach nicht extrahieren. Möglicherweise hängt es mit .first zusammen.
Ich habe die Lösung von Get href link using python playwright implementiert, aber sie funktioniert nicht.
Mobile version