-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp.py
More file actions
56 lines (42 loc) · 1.38 KB
/
Copy pathwp.py
File metadata and controls
56 lines (42 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from playwright.sync_api import sync_playwright
import pandas as pd
import urllib.parse
import re
from dateutil import parser
KEYWORD = "brics"
query = f"{KEYWORD} site:washingtonpost.com after:2024-01-01 before:2026-12-31"
results = []
def extract_date(text):
try:
match = re.search(r'\w{3,9} \d{1,2}, \d{4}', text)
if match:
return parser.parse(match.group())
except:
return None
with sync_playwright() as p:
browser = p.firefox.launch(headless=True)
page = browser.new_page()
url = f"https://www.google.com/search?q={urllib.parse.quote(query)}"
page.goto(url)
page.screenshot(path="debug.png")
page.wait_for_timeout(5000)
cards = page.query_selector_all("div.tF2Cxc")
for c in cards:
try:
title = c.query_selector("h3").inner_text()
link = c.query_selector("a").get_attribute("href")
snippet_el = c.query_selector(".VwiC3b")
snippet = snippet_el.inner_text() if snippet_el else ""
date = extract_date(snippet)
results.append({
"title": title,
"url": link,
"date": date,
"snippet": snippet
})
except:
continue
browser.close()
df = pd.DataFrame(results)
df.to_excel("washingtonpost_brics.xlsx", index=False)
print("Найдено:", len(df))