-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttScraper.py
162 lines (133 loc) · 6.18 KB
/
ttScraper.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
import time
import sys
import subprocess
import random
import re
intermediary_article_list = []
master_article_list = []
master_title_list = []
def proxy_disconnect():
disconnect = subprocess.Popen(['windscribe', 'disconnect'], stdout=subprocess.PIPE)
d_output_raw = str(disconnect.communicate())
regex = r'(\w*)'
d_output_final = str(re.findall(regex, d_output_raw)).replace(",", "").replace("'", "").replace(u'x08', "")
disconnected_proxy = "DISCONNECTED" in d_output_final
if disconnected_proxy:
print("successfully disconnected from proxy")
else:
print("URGENT: unable to disconnect, kill process")
sys.exit()
def intermediary_article_tracker():
print("Intermediary article list length:" + str(len(intermediary_article_list)))
# print(str(intermediary_article_list))
if len(intermediary_article_list) >= 4:
print("Maximum length of intermediary articles reached. Transferring articles to master list")
for i in intermediary_article_list:
master_article_list.append(i)
print("master article list appended")
intermediary_article_list.clear()
print("Intermediary articles cleared.")
print("master article list:" + str(master_article_list))
for i in master_article_list:
with open('ttArticles.csv', 'a') as wtArticles:
wtArticles.write(str(i) + ',')
print("Master article written to CSV.")
master_article_list.clear()
print("Master articles cleared.")
proxy_disconnect()
else:
fill_temp_articles()
def fill_temp_articles():
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options, executable_path=r'/path_to/geckodriver')
print("Loading Toutiao main page.")
driver.get('http://www.toutiao.com')
driver.get("https://www.toutiao.com")
driver.find_element_by_xpath("//input[@class='tt-input__inner']").send_keys("美国")
driver.find_element_by_xpath("//button[@type='button']").click()
print("Search terms entered. Loading results")
driver.close()
driver.switch_to.window(driver.window_handles[0])
print("Sleeping 10 seconds.")
time.sleep(10)
temp_articles_list = []
links = driver.find_elements_by_xpath("//*[@class='link title']")
for link in links:
try:
link.click()
time.sleep(10)
print("First related article clicked.")
print("Sleeping 10 seconds.")
driver.switch_to.window(driver.window_handles[1])
print("temporary articles list:" + str(temp_articles_list))
title = driver.find_element_by_xpath("//h1[@class='article-title']").text
with open('ttTitles.csv', 'r') as rttitles:
rrtitles = rttitles.read()
if title in rrtitles:
print("Title already in ttTitles.csv")
driver.close()
driver.switch_to.window(driver.window_handles[0])
else:
with open('ttTitles.csv', 'a') as wttitles:
wttitles.write(title + ",")
print(title + " written to CSV.")
body_element_list = driver.find_elements_by_xpath("//p")
body_list = []
for p in body_element_list:
body_list.append(p.text)
body = str(body_list).replace(",", "").replace("'", "").replace(u'\\u200c', "")
article_dict = {title: body}
if article_dict in temp_articles_list:
pass
elif article_dict in intermediary_article_list:
pass
elif article_dict in master_article_list:
pass
else:
temp_articles_list.append(article_dict)
print("Article added to temporary article list:" + title)
print("temporary article list:" + str(temp_articles_list))
if len(temp_articles_list) >= 2:
print("Maximum length for temporary articles reached.")
for i in temp_articles_list:
intermediary_article_list.append(i)
print("Intermediary articles appended.")
temp_articles_list.clear()
print("Temporary articles cleared.")
intermediary_article_tracker()
print("closing window")
driver.close()
driver.switch_to.window(driver.window_handles[0])
except NoSuchElementException:
print("Article unavailable.")
driver.close()
driver.switch_to.window(driver.window_handles[0])
except StaleElementReferenceException:
print("Stale element.")
driver.close()
driver.switch_to.window(driver.window_handles[0])
def proxy_connect():
server_list = ["Paris", "Frankfurt", "Amsterdam", "Oslo", "Bucharest", "Zurich"]
random_location = random.choice(server_list)
print("connecting to random server")
random_connect = subprocess.Popen(['windscribe', 'connect', random_location], stdout=subprocess.PIPE)
rc_output_raw = str(random_connect.communicate())
regex = r'(\w*)'
rc_output_final = str(re.findall(regex, rc_output_raw)).replace(",", "").replace("'", "").replace(u'x08', "")
proxy_connected = "Connected" in str(rc_output_final)
if proxy_connected:
print("successfully connected to proxy")
for i in server_list:
if i in rc_output_final:
print("server: " + i)
else:
pass
fill_temp_articles()
else:
print("connection unsuccessful")
# fill_temp_articles()
proxy_connect()