scrapy 抓取多个页面,提取数据并保存到 mysql
scrapy crawl multiple pages, extracting data and saving into mysql
嗨,有人可以帮助我吗?我似乎被卡住了,我正在学习如何抓取并保存到 mysql us scrapy。我正在尝试抓取所有网站页面。从 "start_urls" 开始,但它似乎并没有自动抓取所有页面,只有一个页面,它确实保存到 mysql 和 pipelines.py。当在 f = open("urls.txt") 中提供 url 时,它也会抓取所有页面,并使用 pipelines.py.
保存数据
这是我的代码
test.py
import scrapy
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.selector import HtmlXPathSelector
from gotp.items import GotPItem
from scrapy.log import *
from gotp.settings import *
from gotp.items import *
class GotP(CrawlSpider):
name = "gotp"
allowed_domains = ["www.craigslist.org"]
start_urls = ["http://sfbay.craigslist.org/search/sss"]
rules = [
Rule(SgmlLinkExtractor(
allow=('')),
callback ="parse",
follow=True
)
]
def parse(self, response):
hxs = HtmlXPathSelector(response)
prices = hxs.select("//div[@class="sliderforward arrow"]")
for price in prices:
item = GotPItem()
item ["price"] = price.select("text()").extract()
yield item
如果我没理解错的话,你是在尝试按照分页提取结果。
在这种情况下,您可以避免使用 CrawlSpider
并使用常规 Spider
class。
我们的想法是解析第一页,提取总结果数,计算要走的页数,并为提供 s
GET 参数的相同 URL 产生 scrapy.Request
个实例值。
实现示例:
import scrapy
class GotP(scrapy.Spider):
name = "gotp"
allowed_domains = ["www.sfbay.craigslist.org"]
start_urls = ["http://sfbay.craigslist.org/search/sss"]
results_per_page = 100
def parse(self, response):
total_count = int(response.xpath('//span[@class="totalcount"]/text()').extract()[0])
for page in xrange(0, total_count, self.results_per_page):
yield scrapy.Request("http://sfbay.craigslist.org/search/sss?s=%s&" % page, callback=self.parse_result, dont_filter=True)
def parse_result(self, response):
results = response.xpath("//p[@data-pid]")
for result in results:
try:
print result.xpath(".//span[@class='price']/text()").extract()[0]
except IndexError:
print "Unknown price"
这将遵循控制台上的分页和打印价格。希望这是一个好的起点。
嗨,有人可以帮助我吗?我似乎被卡住了,我正在学习如何抓取并保存到 mysql us scrapy。我正在尝试抓取所有网站页面。从 "start_urls" 开始,但它似乎并没有自动抓取所有页面,只有一个页面,它确实保存到 mysql 和 pipelines.py。当在 f = open("urls.txt") 中提供 url 时,它也会抓取所有页面,并使用 pipelines.py.
保存数据这是我的代码
test.py
import scrapy
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.selector import HtmlXPathSelector
from gotp.items import GotPItem
from scrapy.log import *
from gotp.settings import *
from gotp.items import *
class GotP(CrawlSpider):
name = "gotp"
allowed_domains = ["www.craigslist.org"]
start_urls = ["http://sfbay.craigslist.org/search/sss"]
rules = [
Rule(SgmlLinkExtractor(
allow=('')),
callback ="parse",
follow=True
)
]
def parse(self, response):
hxs = HtmlXPathSelector(response)
prices = hxs.select("//div[@class="sliderforward arrow"]")
for price in prices:
item = GotPItem()
item ["price"] = price.select("text()").extract()
yield item
如果我没理解错的话,你是在尝试按照分页提取结果。
在这种情况下,您可以避免使用 CrawlSpider
并使用常规 Spider
class。
我们的想法是解析第一页,提取总结果数,计算要走的页数,并为提供 s
GET 参数的相同 URL 产生 scrapy.Request
个实例值。
实现示例:
import scrapy
class GotP(scrapy.Spider):
name = "gotp"
allowed_domains = ["www.sfbay.craigslist.org"]
start_urls = ["http://sfbay.craigslist.org/search/sss"]
results_per_page = 100
def parse(self, response):
total_count = int(response.xpath('//span[@class="totalcount"]/text()').extract()[0])
for page in xrange(0, total_count, self.results_per_page):
yield scrapy.Request("http://sfbay.craigslist.org/search/sss?s=%s&" % page, callback=self.parse_result, dont_filter=True)
def parse_result(self, response):
results = response.xpath("//p[@data-pid]")
for result in results:
try:
print result.xpath(".//span[@class='price']/text()").extract()[0]
except IndexError:
print "Unknown price"
这将遵循控制台上的分页和打印价格。希望这是一个好的起点。