【发布时间】:2019-07-10 01:00:26
【问题描述】:
我是 scrapy 的新手,正在编写我的第一个蜘蛛,为类似于 https://blogs.webmd.com/diabetes/default.htm 的网站制作了一个 scrapy 蜘蛛
我想抓取标题,然后导航到每篇文章,抓取每篇文章的文本内容。
我尝试过使用规则和链接提取器,但它无法导航到下一页并提取。我得到错误:蜘蛛错误处理 https://blogs.webmd.com/diabetes/default.htm>(参考:无)
下面是我的代码
import scrapy
from scrapy.spiders import Rule
from scrapy.linkextractors import LinkExtractor
class MedicalSpider(scrapy.Spider):
name = 'medical'
allowed_domains = ['https://blogs.webmd.com/diabetes/default.htm']
start_urls = ['https://blogs.webmd.com/diabetes/default.htm']
Rules = (Rule(LinkExtractor(allow=(), restrict_css=('.posts-list-post-content a ::attr(href)')), callback="parse", follow=True),)
def parse(self, response):
headline = response.css('.posts-list-post-content::text').extract()
body = response.css('.posts-list-post-desc::text').extract()
print("%s : %s" % (headline, body))
next_page = response.css('.posts-list-post-content a ::attr(href)').extract()
if next_page:
next_href = next_page[0]
next_page_url = next_href
request = scrapy.Request(url=next_page_url)
yield request
请指导一个scrapy的新手,让这个蜘蛛正确地处理每个页面上的多篇文章。
【问题讨论】:
标签: python-3.x scrapy