Python实现抓取糗事百科的段子

时间:2022-04-06 18:32:58

原文网址http://python.jobbole.com/81351/#article-comment

原网址正则表达式并不能准确的获取糗事百科的段子

对正则表达式修改后的代码

# -*- coding:utf-8 -*-

import urllib
import urllib2
import re

 

#获取糗百的第一页

page = 1

#网址路径

url = 'http://www.qiushibaike.com/hot/page/' + str(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)
    content = response.read().decode('utf-8')
    pattern = re.compile('<div.*?class="author.*?>.*?<a.*?</a>.*?<a.*?>(.*?)</a>.*?<div.*?class'+
                         '="content".*?>(.*?)</div>(.*?)<div class="stats.*?class="number">(.*?)</i>',re.S)
    items = re.findall(pattern,content)
    for item in items:
        haveImg = re.search("img",item[3])
        if not haveImg:
            print item[0].strip('<h2>'),item[1],item[3]
except urllib2.URLError, e:
    if hasattr(e,"code"):
        print e.code
    if hasattr(e,"reason"):
        print e.reason