spiders的使用
生活随笔
收集整理的這篇文章主要介紹了
spiders的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
first_scrapy文件夾下的items.py:
import scrapy
class FirstScrapyItem(scrapy.Item):
? ? # define the fields for your item here like:
? ? # name = scrapy.Field()
? ? name = scrapy.Field()
? ? url = scrapy.Field()
? ? desc = scrapy.Field()
spiders文件夾下的first_spider.py:
import scrapy
from first_scrapy.items import FirstScrapyItem
class firstSpider(scrapy.Spider):
? ? name = "first"
#bu zhong yao
? ? allowed_domains = ["blog.eastmoney.com"]
? ? start_urls = [
? ? ? ? "http://blog.eastmoney.com/xuedaolaozu",
"http://blog.eastmoney.com/sg15837988958sg"
? ? ]
#東方財富網站
? ? def parse(self, response):
? ? ? ? #filename = response.url.split("/")[-1]
? ? ? ? #print 'Curent URL => ', filename
? ? ? ? #with open(filename, 'wb') as f:
? ? ? ? # ? f.write(response.body)
for sel in response.xpath('//div[@class="articleTit"]/span[@class="title"]'):
item = FirstScrapyItem()
? ? ? ? ? ? #item["name"] = sel.xpath('a/text()').extract().encode('utf-8')
item["name"] = sel.xpath('a/text()').extract()
? ? ? ? ? ? item["url"]= sel.xpath('a/@href').extract()
? #wei kong
? ? ? ? ? ? item["desc"] = sel.xpath('text()').extract()
yield item
? ? ? ? ? ??
總結
以上是生活随笔為你收集整理的spiders的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Maven实战-许晓斌]-[第二章]-
- 下一篇: c语言鸡尾酒排序的例子,经典算法——鸡尾