Aşağıda ki kodun 999999 (misal) kere çalışmasını istediğimde, articleden önce ki 3. div'e de o yazılsın.
Yardımcı olur musunuz?
1. çalıştırmasında,
xpath_expressions = ["//body/div[@id='react-root']/div[1]/div[1]/div[2]/main[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[1]/section[1]/div[1]/div[1]/
div[1]/div[1]/div[1]/article[1]/div[1]/div[1]/div[2]/div[2]"]
2. çalışmasında,
xpath_expressions = ["//body/div[@id='react-root']/div[1]/div[1]/div[2]/main[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[1]/section[1]/div[1]/div[1]/
div[2]/div[1]/div[1]/article[1]/div[1]/div[1]/div[2]/div[2]"]
3. çalışmasında,
xpath_expressions = ["//body/div[@id='react-root']/div[1]/div[1]/div[2]/main[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[1]/section[1]/div[1]/div[1]/
div[3]/div[1]/div[1]/article[1]/div[1]/div[1]/div[2]/div[2]"]
...
999999. çalışmasında
xpath_expressions = ["//body/div[@id='react-root']/div[1]/div[1]/div[2]/main[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[1]/section[1]/div[1]/div[1]/
div[999999]/div[1]/div[1]/article[1]/div[1]/div[1]/div[2]/div[2]"]
# XPath ifadeleri dizisi
xpath_expressions = [
"//body/div[@id='react-root']/div[1]/div[1]/div[2]/main[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[1]/section[1]/div[1]/div[1]/div[1]/div[1]/div[1]/article[1]/div[1]/div[1]/div[2]/div[2]",
"//body/div[@id='react-root']/div[1]/div[1]/div[2]/main[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/div[1]/article[1]/div[1]/div[1]/div[2]/div[2]",
"//body/div[@id='react-root']/div[1]/div[1]/div[2]/main[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[3]/div[1]/div[1]/section[1]/div[1]/div[1]/div[3]/div[1]/div[1]/article[1]/div[1]/div[1]/div[2]/div[2]"
]
# Sonuçları tutacak boş bir liste oluştur
all_tweets = []
# Her XPath ifadesi için tweetleri çek
for xpath_exp in xpath_expressions:
tweets = driver.find_elements(By.XPATH, xpath_exp)
all_tweets.extend(tweets)
# Son 10 tweet için döngü
for tweet in all_tweets[:10]:
# Görselin URL'sini bul
try:
image_element = tweet.find_element(By.XPATH, ".//img")
image_url = image_element.get_attribute('src')
except:
image_url = None
# Tweet'in metnini bul
text_element = tweet.find_element(By.XPATH, ".//div[@lang='tr']")
tweet_text = text_element.text
# Görseli indir
if image_url:
response = requests.get(image_url)
if response.status_code == 200:
# Görseli kaydet
with open(f'images/{tweet.id}.jpg', 'wb') as file:
file.write(response.content)
# Tweet'in metnini ve görselini yazdır
print("Tweet Metni:", tweet_text)
print("Görsel URLsi:", image_url)
print("\n")
# Tarayıcıyı kapatın
driver.quit()