Nis*_*son 3 python scrapy web-scraping
我想用scrapy刮一个wordpress网站。我的问题是我想要标题、文本、日期和作者。作者数据不打印在主要文章上,全文也不在简短版本中。所以我必须先复制作者,然后访问帖子的完整版本以获取文本。我不知道如何将数据从两个 url 发送到同一 csv 行。
所以我想访问https://www.exemple.me/news/page/1/ copy author --> 转到第一篇文章复制标题、日期和文本 --> 将数据存储到 csv(作者、标题、 date,text,) --> 返回https://www.exemple.me/news/page/1/并在第二篇文章中做同样的事情,依此类推..
我知道如何使用选择器,所以我的问题是我无法将数据从两个 url 存储到同一行。
我可以用 selenium 和 BeautifulSoup 做到这一点,但想学习如何在 scrapy 中做到
您可以使用cb_kwargs
来传递author
信息:
import scrapy
class WordpressSpider(scrapy.Spider):
name = "wp"
start_urls = ['https://www.wordpresssite.com']
def parse(self, response):
for article in response.xpath('//article/selector'):
author = article.xpath('./author/selector').get()
article_url = article.xpath('./article/url/selector').get()
yield scrapy.Request(
url=article_url,
callback=self.parse_article,
cb_kwargs={
'author': author,
}
)
def parse_article(self, response, author):
title = response.xpath('//title/selector').get()
date = response.xpath('//date/selector').get()
text = response.xpath('//text/selector').get()
yield {
'title': title,
'date': date,
'text': text,
'author': author
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
641 次 |
最近记录: |