-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fetcher.py
287 lines (273 loc) · 9.5 KB
/
Fetcher.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import os
import re
import sys
import html
import time
import random
import hashlib
import os.path
import logging
import selenium
import requests
import datetime
import PyRSS2Gen
import HTMLParser
import urllib.request
from bs4 import BeautifulSoup
from selenium import webdriver
from urllib.parse import quote
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
ISOTIMEFORMAT='%Y-%m-%d %X'
BASE_URL = 'http://weixin.sogou.com'
EXT = ''
UA = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
LOG_PATH="D://websites//tw_wordpress//cronlog.txt"
XML_PATH="D://websites//tw_wordpress//twrss.xml"
REAL_HREF=""
IMG_PATH="D://websites//tw_wordpress//wximg"
def get_html(url):
#chromedriver = 'C:/Users/_/AppData/Local/Google/Chrome/Application/chromedriver.exe'
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
UA
)
dcap["takesScreenshot"] = (False)
try:
options = webdriver.ChromeOptions()
options.add_argument(UA)
browser.get(url + '&ext=' + EXT)
browser.implicitly_wait(30)
time.sleep(1)
#智能等待30秒
WebDriverWait(browser, 10).until(lambda browser : browser.find_element_by_css_selector('#wxmore > a'))
element=WebDriverWait(browser, 10).until(lambda browser : browser.find_element_by_css_selector('#wxmore > a'))
element.click()
time.sleep(1)
except selenium.common.exceptions.WebDriverException:
pass
try:
html = browser.page_source
except Exception as e:
html = None
logging.error(e)
return html
def get_ehtml(url):
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
UA
)
dcap["takesScreenshot"] = (False)
#t0 = time.time()
try:
options = webdriver.ChromeOptions()
options.add_argument(UA)
browser.get(url + '&ext=' + EXT)
WebDriverWait(browser, 10).until(lambda browser : browser.find_element_by_css_selector('#page-content'))
time.sleep(1)
except selenium.common.exceptions.WebDriverException:
pass
try:
html = browser.page_source
global REAL_HREF
REAL_HREF = browser.current_url
except Exception as e:
html = None
logging.error(e)
return html
def get_dhtml(url):
#chromedriver = 'C:/Users/_/AppData/Local/Google/Chrome/Application/chromedriver.exe'
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (UA)
dcap["takesScreenshot"] = (False)
#t0 = time.time()
try:
options = webdriver.ChromeOptions()
options.add_argument(UA)
browser.get(url)
browser.implicitly_wait(2)
except selenium.common.exceptions.WebDriverException:
pass
try:
html = browser.page_source
except Exception as e:
html = None
logging.error(e)
return html
def get_html_direct(url,cookies=None):
if not cookies:
cookies = update_cookies()
headers = {"User-Agent": UA}
r = requests.get(url, headers=headers, cookies=cookies, timeout=20)
r.encoding='utf8'
return r.text
def parse_essay(link):
e = get_ehtml(link)
html = e
soup = BeautifulSoup(html,"html.parser")
essay = {}
p = re.compile(r'\?wx_fmt.+?\"')
d = re.compile(r'data-wxsrc="data.image.png.base64.\S*"')
b = soup.select("#js_content")[0]
content = str(b).replace('src', 'data-wxsrc')
content = content.replace('data-data-wxsrc', 'src')
content = re.sub(p, '"', content)
content = re.sub(d, '', content)
pic_Soup = BeautifulSoup(content,"html.parser")
pic_pool = pic_Soup.find_all('img')
try:
for pic in pic_pool:
link = pic.get('src')
try:
filename = link.replace('/','').replace('\\','')+'.jpg'
except:
filename = link + '.jpg'
filename = filename[-15:]
if not os.path.exists(IMG_PATH+'//'+filename):
urllib.request.urlretrieve(link,IMG_PATH+'//'+filename)
content = content.replace(link,"http://127.0.0.1/wximg/"+filename)
except:
pass
essay['content'] =content
essay['name'] = soup.select('#post-user')[0].text
essay['date'] = soup.select('#post-date')[0].text
return essay
def get_account_info(open_id=None, link=None, cookies=None):
url = None
if open_id:
url = BASE_URL + '/gzh?openid=' + open_id
if link:
url = link
#html = get_html(url)
#html = get_html_direct(url, cookies=cookies)
html = get_dhtml(url)
#print(html)
if not html:
return None
soup = BeautifulSoup(html,"html.parser")
try:
info_box = soup.select('#weixinname')[0].parent
except:
fp.write("\n达到搜狗限制,结束任务")
fp.close()
exit('OUT OF TODAY RANGE!')
account_info = {}
account_info['account'] = info_box.select('h4 span')[0].text.split(':')[1].strip()
account_info['name'] = info_box.select('#weixinname')[0].text
account_info['address'] = url
account_info['description'] = info_box.select('.sp-txt')[0].text
img_list = soup.select('.pos-box img')
account_info['logo'] = soup.select(".img-box img")[0]['src']
global rss
rss=PyRSS2Gen.RSS2(
title=account_info['name'],
link="/" + get_md5_value(str(account_info['name']).encode("utf8")),
description = account_info['description'],
pubDate = account_info['description'],
items=[]
);
return account_info
def get_md5_value(src):
myMd5 = hashlib.md5()
myMd5.update(src)
myMd5_Digest = myMd5.hexdigest()
return myMd5_Digest
def parse_list(open_id=None, link=None):
if open_id:
url = BASE_URL + '/gzh?openid=' + open_id
elif link:
url = link
else:
return None
html = get_html(url)
if not html:
return None
soup = BeautifulSoup(html,"html.parser")
ls = soup.select('#wxbox .txt-box')
link_list = []
num = len(ls)
pnum = 1
print('Got:'+str(num))
fp.write("\nGot:"+str(num)+"||")
for item in ls:
print('Process:'+str(pnum))
essay = parse_essay(BASE_URL + item.a['href'])
rss.items.append(PyRSS2Gen.RSSItem(
title = item.a.text,
link = "/" + get_md5_value(str(item.a.text).encode("utf8")),
description = essay['content'],
pubDate = essay['date']));
item_dict = {}
item_dict['title'] = item.a.text
item_dict['link'] = item.a['href']
if (pnum) == num:
fp.write("LastOne")
else:
if (pnum) == 1:
fp.write(item.a.text + "(" + str(essay['date']) +")-")
else:
fp.write(str(pnum)+"-")
pnum+=1
fp.write("\nSaveXml")
try:
rss.write_xml(open(XML_PATH, "w",encoding='utf-8'))
except:
fp.write(".....Failed!")
else:
fp.write(".....Done!")
def weixin_search(name, cookies=None):
url = BASE_URL + '/weixin?query=' + name
#html = get_html(url)
html = get_html_direct(url, cookies=cookies)
soup = BeautifulSoup(html,"html.parser")
ls = soup.select("._item")
search_list = []
for item in ls:
account_info = {}
account_info['account'] = item.select('h4 span')[0].text.split(':')[1].strip()
account_info['name'] = item.select('.txt-box h3')[0].text
if account_info['name'] == name:
tmp_ext = item['href'].split('ext=')[1]
try:
return str(tmp_ext)
except:
fname = str(int(time.time()))
browser.save_screenshot(fname+".png")
fp.write("\n获取EXT参数出错,截图为:"+fname+".png");
exit('Error On Fetch EXT')
def update_cookies():
s = requests.Session()
s.cookies.clear()
s.cookies.clear_session_cookies()
headers = {"User-Agent": UA}
s.headers.update(headers)
url = BASE_URL + '/weixin?query=123'
r = s.get(url)
if 'SNUID' not in s.cookies:
p = re.compile(r'(?<=SNUID=)\w+')
s.cookies['SNUID'] = p.findall(r.text)[0]
suv = ''.join([str(int(time.time()*10000) + random.randint(0, 1000))])
s.cookies['SUV'] = suv
return s.cookies
if __name__ == '__main__':
global fp
fp = open(LOG_PATH,"a",encoding='utf-8') #打开一个文本文件
fp.write("\n------------------------------------")
fp.write("\n任务启动:\n任务类型:创建RSS文件\n时间:" + time.strftime( ISOTIMEFORMAT, time.localtime()))
phantomJSdriver =sys.path[0] + '/bin/phantomjs.exe'
global browser
#browser = webdriver.Chrome(chromedriver)
browser = webdriver.PhantomJS(phantomJSdriver)
browser.delete_all_cookies()
try:
open_id = 'oIWsFtzTQD0avasLckjN1Yz0bqW4'
fp.write("\nopenid ="+open_id);
cookies = update_cookies()
fp.write("\n"+str(get_account_info(open_id,cookies=cookies)));
EXT = weixin_search("湖理青年",cookies)
fp.write("\next="+EXT)
parse_list(open_id)
fp.write("\n任务成功完成。")
finally:
fp.close()
browser.quit()