-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitterVeriCekme.py
146 lines (94 loc) · 4.46 KB
/
TwitterVeriCekme.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from bs4 import BeautifulSoup
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import csv
def veri_cek():
sayfa = int(input("scroll sayısını girin = "))
driver_path = "C:\Anaconda3\Lib\chromedriver.exe"
browser = webdriver.Chrome(driver_path)
browser.get("https://www.google.com.tr/")
yazi_girisi = browser.find_element_by_css_selector(".gLFyf.gsfi")
yazi_girisi.send_keys("tc sağlık bakanlığı twitter")
time.sleep(2)
yazi_girisi.send_keys(Keys.ENTER)
tikla = browser.find_element_by_css_selector(".NsiYH")
tikla.click()
#
file = open("tweetler.csv", "w", encoding="utf-8")
writer = csv.writer(file)
writer.writerow(["Tweetler", "YorumSayisi", "RetweetSayisi", "BegeniSayisi"])
#
a = 0
while a < sayfa:
lastHeight = browser.execute_script("return document.body.scrollHeight")
i = 0
while i < 1:
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
newHeight = browser.execute_script("return document.body.scrollHeight")
if newHeight == lastHeight:
break
else:
lastHeight = newHeight
i = i + 1
sayfa_kaynagi = browser.page_source
soup = BeautifulSoup(sayfa_kaynagi, "html.parser")
tweetler = soup.find_all("div", attrs={"data-testid": "tweet"})
for i in tweetler:
try:
yazi = i.find("div", attrs={
"class": "css-1dbjc4n r-1iusvr4 r-16y2uox r-1777fci r-1mi0q7o"}).text
print(i.find("div", attrs={
"class": "css-1dbjc4n r-1iusvr4 r-16y2uox r-1777fci r-1mi0q7o"}).text)
yorum_sayisi = i.find("div", attrs={"data-testid": "reply"}).text
retweet_sayisi = i.find("div", attrs={"data-testid": "retweet"}).text
begeni_sayisi = i.find("div", attrs={"data-testid": "like"}).text
writer.writerow([yazi, yorum_sayisi, retweet_sayisi, begeni_sayisi])
except:
print("**")
a = a + 1
veri_cek()
###########################################################################################################################################################
import time
import login
from selenium import webdriver
driver_path = "C:\Anaconda3\Lib\chromedriver.exe"
browser = webdriver.Chrome(executable_path=driver_path)
browser.get("https://twitter.com/")
time.sleep(3)
giris_yap = browser.find_element_by_xpath("//*[@id='react-root']/div/div/div/main/div/div/div/div[1]/div/a[2]")
giris_yap.click()
time.sleep(5)
username = browser.find_element_by_xpath("//*[@id='react-root']/div/div/div[2]/main/div/div/div[1]/form/div/div[1]/label/div/div[2]/div/input")
password = browser.find_element_by_xpath("//*[@id='react-root'']/div/div/div[2]/main/div/div/div[1]/form/div/div[2]/label/div/div[2]/div/input")
time.sleep(3)
login = browser.find_element_by_xpath("//*[@id='react-root']/div/div/div[2]/main/div/div/div[1]/form/div/div[3]/div/div")
login.click()
time.sleep(5)
searchArea = browser.find_element_by_xpath("//*[@id='react-root']/div/div/div[2]/main/div/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[2]/div/div/div/form/div[1]/div/div/div[2]/input")
searchButton = browser.find_element_by_xpath("//*[@id='global-nav-search']/span/button")
searchArea.send_keys("#ArtificialIntelligence")
searchArea.click()
time.sleep(5)
lenOfPage = browser.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
match=False
while(match==False):
lastCount = lenOfPage
time.sleep(3)
lenOfPage = browser.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
if lastCount == lenOfPage:
match=True
time.sleep(5)
tweets = []
elements = browser.find_elements_by_css_selector(".TweetTextSize.js-tweet-text.tweet-text")
for element in elements:
tweets.append(element.text)
tweetCount = 1
with open("ai.txt","w",encoding = "UTF-8") as file:
for tweet in tweets:
file.write(str(tweetCount) + ".\n" + tweet + "\n")
file.write("**************************\n")
tweetCount += 1
browser.close()