抓取网站时收集不同的属性

问题描述 投票:1回答:1

我一直在为不同的网站创建蜘蛛,其中包含输出原始文本,文本和URL的文章。我想获取更多信息,例如描述,语言,发布日期等。

这是一个示例:

import scrapy
import re
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import requests
from bs4 import BeautifulSoup
stop_words = set(stopwords.words("german"))


class AListSpider(scrapy.Spider):
    name = 'aList'
    r = requests.get("https://www.a-list.at/newssitemap.xml")
    xml = r.text
    soup = BeautifulSoup(xml)
    urls_wien = list(filter(lambda y: '/wien/' in y, map(lambda x: x.text, soup.find_all("loc"))))

    def start_requests(self):
        urls = self.urls_wien
        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        try:
            yield {
              'raw_text': BeautifulSoup(response.css('.news-single-item').get(), "html.parser").find(class_="news-single-item").text,
              'text': ' '.join([i for i in word_tokenize(re.sub(pattern='[^a-zA-Z_\-ÖöÜüÄäßèé]',string=BeautifulSoup(response.css('.news-single-item').get(), "html.parser").find(class_="news-single-item").text, repl=' ')) if i not in stop_words and not re.match('[0-9]', i) and len(i) >1]),
              'url': response.url
          }
        except:
            pass

我已经阅读到可以使用schema.org收集更多信息,请任何人解释一下它是如何工作的?

python web-crawler schema.org
1个回答
2
投票

在我的上一个项目中,我想验证在收集时刮掉的物品,因此我使用了scrapy_jsonschema。他们的页面很好地解释了如何使用它。

如果需要帮助来构建json模式,此站点真的很有帮助http://json-schema.org/learn/getting-started-step-by-step.html

© www.soinside.com 2019 - 2024. All rights reserved.