-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·94 lines (89 loc) · 2.28 KB
/
test.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# python
#from urllib import urlopen
import urllib2
import ssl
from BeautifulSoup import BeautifulSoup
import requests
import os
import errno
import re
import random
import socket
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno==errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def shownews(number):
url="https://dapenti.com/blog/more.asp?name=tupian&id="+str(number)
try:
context=ssl._create_unverified_context()
webpagesrc=urllib2.urlopen(url, context=context).read()
uhtml=unicode(webpagesrc,"gbk")
soup=BeautifulSoup(uhtml)
srcend=soup.originalEncoding
ps=soup.findAll('p')
for p in ps:
imgp=p.find("img")
if (imgp):
textp=p.find("strong").parent
break
else:
continue
if (imgp):
imgsrc=imgp['src']
picturedir="/home/qihao/cherry/pentitugua/picture"
if not os.path.exists(picturedir):
mkdir_p(picturedir)
downloadpicpath=picturedir+"/"+str(number)+".jpg"
sentencefile="/home/qihao/cherry/pentitugua/sentence.txt"
if os.path.exists(downloadpicpath):
pass
else:
print "Downloading, please wait..."
os.system("wget "+imgsrc+" -qO "+downloadpicpath+
" && convert -resize 300 "+downloadpicpath+" "+downloadpicpath)
thetext=textp.renderContents()
print "------------------------------------------------------------------"
os.system("tycat "+downloadpicpath)
showtext=re.sub(" ","",re.sub("<[^>]*?>","",thetext))
show=""
for s in showtext.splitlines():
s=s.rstrip()
show+=s
print show
if (show!=""):
sentenceToFile="["+str(number)+"] "+show+"\n"
fp=open(sentencefile,'a+')
fp.write(sentenceToFile)
fp.close()
print "------------------------------------------------------------------"
return True
else:
print "nothing find"
return False
except Exception,e:
print e
return False
try:
socket.setdefaulttimeout(30)
startNumber=input("Please input begin search url id number: ")
stopNumber=input("Pleaser input end search url id number: ")
for i in range(startNumber, stopNumber+1):
ret=shownews(i)
if ret:
answer=raw_input("Next[y/n, defalut:y]? : ") or 'y'
if (answer=='y'):
continue
else:
os._exit(0)
else:
continue
os._exit(0)
except Exception,e:
os._exit(-1)