python-3.x - 使用 Scrapy 获取所有文章
问题描述
我将此脚本与 Scrapy 一起使用:
import scrapy
class PageSpider(scrapy.Spider):
name = "page"
start_urls = ['http://blog.theodo.com/']
def parse(self, response):
for article_url in response.css('.Link-sc-19p3alm-0 fnuPWK a ::attr("href")').extract():
yield response.follow(article_url, callback=self.parse_article)
def parse_article(self, response):
content = response.xpath(".//div[@class='entry-content']/descendant::text()").extract()
yield {'article': ''.join(content)}
我正在学习教程,但我猜有些部分需要更改。
我已经改变了:
response.css('.Link-sc-19p3alm-0 fnuPWK a ::attr("href")').extract():
我想这是我获取文章链接所需要的->
但我坚持使用xpath。文章的所有内容都包含在一个 div 中,但不再有 entry-content :
我想知道我是否在 response.css 中放入了正确的东西,以及我需要在 xpath 中编写的路径并理解其背后的逻辑。
谢谢你,我希望我的帖子很清楚:)
解决方案
打开你的终端,写下scrapy shell 'blog.theodo.com'
对于你必须做的 href 元素:
response.xpath('//a[@class="Link-sc-19p3alm-0 fnuPWK"]/@href').get()
我也不能给你一个“文本”的例子,因为你的图片没有为我显示足够的信息。
还要记住:如果你使用 ' 作为你的第一个引号,你必须在 class= 之后使用双引号,例如('//div[@class=""]')
对于https://www.formatic-centre.fr/formation/dynamiser-vos-equipes-special-post-confinement/上的整篇文章
response.xpath('//div[@class="course-des-content"]//text()').getall()
.get() 将为您提供第一场比赛,但在这种情况下,getall 更适合 imo
推荐阅读
- authorization - 有没有办法在 Scm-Manager 上设置 SVN 路径读取权限
- c++ - GetDlgItem(IDC_STATIC)->SetFont(&m_font) 抛出异常
- mysql - 如何按两个日期中较早/较早的日期排序 SQL 结果?
- azure - Azure DevOps 中是否可以按区域路径的第一个子层进行分组?
- pdf - 如何将许多情节图放在一个文件中并添加文本
- elasticsearch - Elasticsearch 找不到包含“-”的值
- docker - 运行时访问docker容器的IP
- flutter - Flutter中的DropdownButtonFormField在使用图标时不居中,在其InputDecoration中使用suffixIcon时,将无法按下
- symfony - crudcontroller 中基于当前登录用户或用户 ID 的实体数据
- typescript - 如何在打字稿中访问嵌套条件类型