码迷,mamicode.com
首页 > 编程语言 > 详细

python爬虫1.4

时间:2017-10-18 23:43:59      阅读:426      评论:0      收藏:0      [点我收藏+]

标签:col   pytho   object   content   default   pre   tar   www   int   

import requests
import time
import xml.etree.ElementTree as ET

from multiprocessing.dummy import Pool as ThreadPoo;
from xml.parsers.expat import ParserCreate

class DefaultSaxHandler(object):
    def __init__(self, provinces):
        self.provinces = provinces

    def start_element(self, name, attrs):
        if name != map:
            name = attrs[title]
            number = attrs[href]
            self.provinces.append((name, number))
            
    def end_element(self, name):
        pass

    def char_data(self, text):
        pass
    
def get_provinces(url):
    content = requests.get(url).content.decode(gb2312)
    start = content.find(<map name=\"map_86\" id=\"map_86\">)
    end = content.find(</map>)
    content = content[start:end + len(</map>)].strip()
    print(content)
    provinces = []
    handler = DefaultSaxHandler(provinces)
    parser = ParserCreate()
    parser.StartElementHandler = handler.start_element
    parser.EndElementHandler = handler.end_element
    parser.CharacterDataHandler = handler.char_data
    parser.Parse(content)
    return provinces

provinces = get_provinces(http://www.ip138.com/post)
print(provinces)

 

python爬虫1.4

标签:col   pytho   object   content   default   pre   tar   www   int   

原文地址:http://www.cnblogs.com/xiaoyingying/p/7689841.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!