-
Notifications
You must be signed in to change notification settings - Fork 0
/
20170524.py
57 lines (48 loc) · 1.38 KB
/
20170524.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
#import platform
#print platform.python_version()
#
#import sys
#print sy#s.version
#import random
#a = random.randint(0,9)
#value = input("please input a number:")
#if value == a:
# print "you are right"
#else:
# print "you are wrong,a = %s" %(a)#
#coding=utf-8
import requests
import re
import urllib2
import HTMLParser
url = 'http://daily.zhihu.com/'
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
header = {'User-Agent':user_agent}
def getHtml(url,header):
value = urllib2.Request(url,headers=header)
response = urllib2.urlopen(value)
text = response.read()
return text
def getUrls(url,html):
pattern = re.compile('<a href="/story/(.*?)"')
items = re.findall(pattern,html)
urls = []
for item in items:
urls.append(url+'story/'+item)
return urls
def getContent(url):
html = getHtml(url,header)
pattern = re.compile('<h1 class="headline-title">(.*?)</h1>')
items = re.findall(pattern,html)
print '*********************'+items[0]+'*******************'
pattern = re.compile('<div class="content">\\n<p>(.*?)</div>',re.S)
items1 = re.findall(pattern,html)
for item in items1:
print item
html = getHtml(url,header)
urls = getUrls(url,html)
for url in urls:
try:
getContent(url)
except Exception,e:
pass