安装scrapy出现错误,解决方法为安装openssl
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
pip3 install setuptools
sudo python3 -m pip install pyopenssl
sudo apt-get install python3-openssl
官方例子:
import scrapy
class BlogSpider(scrapy.Spider):
name = 'blogspider'
start_urls = ['https://blog.scrapinghub.com']
def parse(self, response):
for title in response.css('h2.entry-title'):
yield {'title': title.css('a ::text').extract_first()}
next_page = response.css('div.prev-post > a ::attr(href)').extract_first()
if next_page:
yield scrapy.Request(response.urljoin(next_page), callback=self.parse)
获取moozik的标题和提要:
import scrapy
class BlogSpider(scrapy.Spider):
name = 'blogspider'
start_urls = ['http://www.moozik.cn/']
def parse(self, response):
html=response.xpath('/html/body/div[2]/div/div/div')
for res in html:
yield {
'title': res.xpath('div[1]/h1/a/text()').extract(),
'content':res.xpath('div[2]/div/p/text()').extract()
}
#http://brucedone.com/
import scrapy
class BlogSpider(scrapy.Spider):
name = 'blogspider'
start_urls = ['http://brucedone.com/']
def parse(self, response):
html=response.xpath('/html/body/div[3]/div/div/ul/li/div/a[1]/text()')
for res in html:
yield {
'title': res.extract()
}