@classmethod用法(修饰的函数,第一个参数cls默认是类名,调用方法:实例对象或类对象.方法)
class C_mthod(object):
name = "f**k"
def __init__(self,name):
self.name = name
@classmethod
def t_mthod(cls):
print("hello world",cls.name) d = C_mthod("F**K")
C_mthod.t_mthod() ————————————————————
hello world f**k
@classmethod调用类静态方法,无法调用类继承方法
分享一个爬虫方法,仅供参考
#!/usr/bin/env python
# -*- coding: utf-8 -*- import random import requests
from lxml import etree class WanfangSpider(object):
@classmethod
def crawl_with_keyword(cls, keyword):
url = 'http://s.wanfangdata.com.cn/Paper.aspx?q=' + keyword
print url
response = requests.get(url, cls.get_headers())
if response.status_code == 200:
return cls.get_info(response.text)
else:
return None @classmethod
def get_headers(cls):
user_agent = [
'Mozilla / 5.0(compatible;MSIE9.0;Windows NT 6.1;Trident / 5.0',
'Mozilla / 4.0(compatible;MSIE6.0;Windows NT 5.1',
'Mozilla / 5.0(compatible;MSIE7.0;Windows NT 5.1',
'Mozilla / 5.0(compatible;MSIE8.0;Windows NT 6.0',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
]
headers = {
'User-Agent': random.choice(user_agent)
}
return headers @classmethod
def get_info(cls, content):
tree = etree.HTML(content)
divs = tree.xpath('//*[@class="left-record"]')
result = []
for div in divs:
a_dict = {}
url = div.xpath('div/a[@class="title"]/@href')
title = div.xpath('div/a[@class="title"]')[0].xpath('string(.)')
subtitle = div.xpath('div[@class="record-subtitle"]')[0].xpath('string(.)')
# print url, title, subtitle
if not title:
title = None if url:
url = url[0]
else:
url = None if subtitle:
subtitle = subtitle.strip()
else:
subtitle = None
a_dict['url'] = url
a_dict['title'] = title
a_dict['subtitle'] = subtitle
result.append(a_dict)
return result class It199Spider(object):
@classmethod
def crawl_with_keyword(cls, keyword):
url = 'http://www.199it.com/archives/tag/' + keyword
print url
response = requests.get(url, cls.get_headers())
if response.status_code == 200:
return cls.get_info(response.text)
else:
return None @classmethod
def get_headers(cls):
user_agent = [
'Mozilla / 5.0(compatible;MSIE9.0;Windows NT 6.1;Trident / 5.0',
'Mozilla / 4.0(compatible;MSIE6.0;Windows NT 5.1',
'Mozilla / 5.0(compatible;MSIE7.0;Windows NT 5.1',
'Mozilla / 5.0(compatible;MSIE8.0;Windows NT 6.0',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
]
headers = {
'User-Agent': random.choice(user_agent)
}
return headers @classmethod
def get_info(cls, content):
tree = etree.HTML(content)
articles = tree.xpath('//article')
result = []
for article in articles:
a_dict = {} # 提取正文
img_url = article.xpath('div[@class="entry-list-left"]/div/a/img/@src')
title = article.xpath('div[@class="entry-list-right"]/h2/a/text()')
url = article.xpath('div[@class="entry-list-right"]/h2/a/@href')
post_time = article.xpath('div[@class="entry-list-right"]/table/tr/td[2]/text()')
tag = article.xpath('div[@class="entry-list-right"]/table/tr/td[4]/a/text()')
summary = article.xpath('div[@class="entry-list-right"]/p[@class="post-excerpt"]/text()')
print img_url, url, title, post_time, tag, summary # 构造字典
a_dict['img_url'] = img_url[0] if img_url else None
a_dict['title'] = title[0] if title else None
a_dict['url'] = url[0] if url else None
a_dict['post_time'] = post_time[0] if post_time else None
a_dict['tag'] = tag[0] if tag else None
a_dict['summary'] = summary[0] if summary else None
result.append(a_dict)
return result if len(result) < 10 else result[0: 10]
classmethod类方法使用
@staticmethod(不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。调用方法:实例对象或类对象.方法 )
class A(object):
bar = 1
def foo(self):
print('foo') @staticmethod
def static_foo():
print('static_foo')
print(A.bar) @classmethod
def class_foo(cls):
print('class_foo')
print(cls.bar)
cls().foo() A.static_foo()
A.class_foo() ——————————————————————
static_foo
1
class_foo
1
foo