def parse_rating(self, response): body_dic = json.loads(response.body.decode('utf-8')) for rating in body_dic['ratings']: l = ItemLoader(item=RatingItem(), response=response) l.default_output_processor = TakeFirst() # error, change tid to id l.add_value('rid', rating['id']) l.add_value('tid', response.meta['tid']) l.add_value('sid', response.meta['sid']) l.add_value('attendance', rating['attendance']) l.add_value('helpCount', rating['helpCount']) l.add_value('notHelpCount', rating['notHelpCount']) l.add_value('rClarity', rating['rClarity']) l.add_value('rClass', rating['rClass']) l.add_value('rComments', rating['rComments']) l.add_value('rDate', rating['rDate']) l.add_value('rEasy', rating['rEasy']) l.add_value('rErrorMsg', rating['rErrorMsg']) l.add_value('rHelpful', rating['rHelpful']) l.add_value('rInterest', rating['rInterest']) l.add_value('rOverall', rating['rOverall']) l.add_value('rStatus', rating['rStatus']) l.add_value('rTextBookUse', rating['rTextBookUse']) l.add_value('rWouldTakeAgain', rating['rWouldTakeAgain']) l.add_value('takenForCredit', rating['takenForCredit']) l.add_value('teacher', rating['teacher']) l.add_value('teacherGrade', rating['teacherGrade']) keys = l.get_value(rating['teacherRatingTags']) values = [1] * len(keys) l.add_value('tags', json.dumps(dict(zip(keys, values)))) yield l.load_item()
def parse_page2(self, response): relativepath = "/home/zeaslity/Pictures/zhihu/" l = ItemLoader(item=PicCrawlItem()) l.add_css("image_urls", "div.content a img::attr(src)") l.add_css("image_title", "div.page em") path = relativepath + l.get_value("fileName") l.add_value("path", path) for href in response.css("div.page a::attr(href)"): yield response.follow(href, self.parse_page2)
from CommonProduct import Product from scrapy.loader import ItemLoader from scrapy.loader.processors import TakeFirst if __name__ == '__main__': il = ItemLoader(item=Product()) il.add_value('name', [u'Welcome to my', u'<strong>website</strong>']) il.add_value('price', [u'€', u'<span>1000</span>']) item = il.load_item() print(item) #########ItemLoader objects loader = ItemLoader(product=Product()) loader.context['unit'] = 'cm' val = loader.get_value(u'name: foo', TakeFirst(), str.upper, re='name: (.+)') #print(val) loader.add_value('name', u'Color TV') loader.add_value('colours', [u'white', u'blue']) loader.add_value('length', u'100') loader.add_value('name', u'name: foo', TakeFirst(), re='name: (.+)') loader.add_value(None, {'name': u'foo', 'sex': u'male'}) print(loader.load_item())
________________________________________________________________ ________________________________________________________________ - Trả về 1 new Item Loader để nạp Item. Nếu ko có Item cho trước, nó tự động khởi tạo sử dụng class trong 'default_item_class'. - Khi được khởi tạo với parameter là 1 selector hoặc 1 response, ItemLoader cung cấp cơ chế thuận tiện để trích xuất dữ liệu từ các website mà chúng sử dụng selectors. 'Parameters': |item (Item object) – The item instance to populate using subsequent calls to add_xpath(), add_css(), or add_value(). |selector (Selector object) – The selector to extract data from, when using the add_xpath() (resp. add_css()) or replace_xpath() (resp. replace_css()) method. |response (Response object) – The response used to construct the selector using the default_selector_class, unless the selector argument is given, in which case this argument is ignored. - Item, selector, response và các argument chính còn lại được gán cho Loader context (có thể truy nhập qua thuộc tính 'context') - Instance của Itemloader có các phương thức sau : __________________________________________________________ 'get_value(value, *processors, **kwargs)' - Xử lý 'value' cho trước bằng 'processors' và các argument chính - Vd : >>> from scrapy.loader.processors import TakeFirst >>> loader.get_value(u'name: foo', TakeFirst(), unicode.upper, re='name: (.+)') >>> Ra :'FOO` __________________________________________________________ 'add_value(field_name, value, *processors, **kwargs)' - Xử lý và sau đó thêm 'value' đã cho vào trường field_name. - Đầu tiên, value được truyền vào hàm 'get_value()' bởi processors và kwargs, sau đó truyền vào processor input của field_name, và kết quả được thêm vào dữ liệu đã thu thập trước đó cho field_name. Nếu field_name đã chứa dữ liệu đc thu thập, dữ liệu mới sẽ được thêm vào. - Trong trường hợp field_name = None, các giá trị cho các field có thể được đưa vào, và giá trị được xử lý phải là 1 dictinary mà các field đưa vào map tới các giá trị đó. - Vd : loader.add_value(None, {'name': u'foo', 'sex': u'male'}) loader.add_value('name', u'Color TV') loader.add_value('colours', [u'white', u'blue']) loader.add_value('length', u'100') loader.add_value('name', u'name: foo', TakeFirst(), re='name: (.+)') __________________________________________________________ 'replace_value(field_name, value, *processors, **kwargs)' - Tương tự add_value, nhưng nó thay thế giá trị cũ với giá trị mới. __________________________________________________________