Demo entry 6352329

123

   

Submitted by anonymous on Mar 24, 2017 at 10:53
Language: Python. Code size: 934 Bytes.

# _*_ coding:utf-8 _*_
import urllib
import urllib2
import re

url="http://www.qiushibaike.com/hot/page/"
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
try:
    request = urllib2.Request(url,headers = headers)
    response = urllib2.urlopen(request)
except urllib2.URLError, e:
    if hasattr(e,"code"):
        print e.code
    if hasattr(e,"reason"):
        print e.reason
content = response.read().decode('utf-8')


pattern = re.compile('<a href="/users.*?title="(.*?)">.*?<h2>.*?<span>(.*?)</span>.*?</a>(.*?)<div class="stats".*?number">(.*?)</i.*?number">(.*?)</i>',re.S)
items=re.findall(pattern,content)
for item in items:
    br=re.compile('<br/>')
    text = br.sub('\n',item[1])
    imgcheck=re.search('img',item[2])
    if not imgcheck:
        print 'author: %s\n%s\n%s people like it, with %s comments\n'%(item[0],text,item[3],item[4])

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).