-
Notifications
You must be signed in to change notification settings - Fork 0
/
taogu_zhanfa.py
146 lines (126 loc) · 3.88 KB
/
taogu_zhanfa.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
# from selenium import webdriver
import xlwt, time
import marshal
import gevent.monkey
gevent.monkey.patch_all()
import requests
import grequests
import csv
import json
from openpyxl import Workbook
import base64
#from PDFWriter import PDFWriter
#import pdfkit
import warnings
from weasyprint import HTML, CSS
from weasyprint.fonts import FontConfiguration
warnings.filterwarnings(action="ignore", module=".*grequests.*")
warnings.filterwarnings(action="ignore", module=".*urllib3.*")
serach = 'https://www.taoguba.com.cn/getSearchTopicResult?pageNo=%s&searchDate=6'
urls = []
for c in range(1, 68):
b = serach % c + '&subject=%E6%88%98%E6%B3%95&type=2'
urls.append(b)
def my_exception_handler(req, e):
print(req)
print(e)
print(dir(req))
print(dir(e))
MAX_CONNECTIONS = 50 # Number of connections you want to limit it to
all = []
pages = len(urls)
for i in range(1, pages + 1, MAX_CONNECTIONS):
print("1 Waiting %s" % i) # Optional, so you see something is done.
rs = (grequests.get(u, timeout=1000, verify=False) for u in urls[i:i + MAX_CONNECTIONS])
a = list(rs)
time.sleep(0.2) # You can change this to whatever you see works better.
results = grequests.map(a, exception_handler=my_exception_handler) # The key here is to extend, not append, not insert.
print("result1 : %s" % len(results))
print(results)
for x in results:
if x:
print(x.status_code)
try:
js = json.loads(x.text)
for j in js['dto']['topicAttr']:
all.append('https://www.taoguba.com.cn/Article/%s/1' % j['topicID'])
except:
pass
x.close()
if i == 1:
#break
pass
def get_as_base64(url):
return base64.b64encode(requests.get(url).content)
all_html = {}
pages = len(all)
print("total %s"%pages)
print(all)
done_all= []
try:
done = open("done", 'rb')
done_all=marshal.load(done)
done.close()
except:
pass
for a in all:
if a in done_all:
all.remove(a)
else:
done_all.append(a)
for i in range(1, pages + 1, MAX_CONNECTIONS):
print("2 Waiting %s" % i) # Optional, so you see something is done.
rs = (grequests.get(u, verify=False, timeout=1000) for u in all[i:i + MAX_CONNECTIONS])
time.sleep(0.2) # You can change this to whatever you see works better.
results = grequests.map(rs, exception_handler=my_exception_handler) # The key here is to extend, not append, not insert.
#print("result2 : %s" % len(results))
for x in results:
if x:
#print(x.status_code)
try:
soup = BeautifulSoup(x.text, 'html.parser')
content = soup.find('div',class_='p_coten')
for img in content.find_all('img'):
t = img.attrs['src']
if t.find('placeholder') != -1:
t = img.attrs['src2']
img.attrs['src'] = "data:image;base64,%s" % get_as_base64(t).decode("utf-8")
all_html[x.url] = content.prettify()
except:
pass
x.close()
if i == 1:
#break
pass
done = open("done", 'wb')
marshal.dump(done_all, done)
done.close()
html = '''<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
'''
for k in all_html:
html += k + '<br>' + all_html[k] + '<hr>'
html += "</body></html>"
font_config = FontConfiguration()
css = CSS(string='''
* {
font-size : 0.8rem;
}
body {
backgroud: black;
}
@font-face {
font-family: Gentium;
src: url(http://example.com/fonts/Gentium.otf);
}
h1 { font-family: Gentium }
img {width: 30; height: 60}''', font_config=font_config)
#print(html)
report_html = HTML(string=html)
report_html.write_pdf(target='test.pdf', stylesheets=[css],
font_config=font_config)