【问题标题】:Scrape data from div as shown on the page从 div 中抓取数据,如页面所示
【发布时间】:2021-08-04 20:26:01
【问题描述】:

我正在尝试从该 URL https://eksisozluk.com/mortingen-sitraze--1277239 抓取数据,我想抓取标题,然后抓取标题下方的所有 cmets。如果您打开网站,您会看到标题下的第一条评论是(bkz: mortingen)。问题是 (bkz 位于 div 中,而 div 内部 mortingen 位于锚链接中,因此很难抓取网站上显示的数据。任何人都可以帮助我使用可以刮掉所有 cmets 的 CSS 选择器或 Xpath,如图所示。 我的代码写在下面,但它给了我 (bkz: 一列然后 akhisar 然后 ) 三列而不是一列 p>

def parse(self, response):
    data={}
    #count=0
    title = response.css('[itemprop="name"]::text').get()
    #data["Title"] = title
    count=0
    data["title"] = title
    count=0
    for content in response.css('li .content ::text'):
        text = content.get()
        text=text.strip()
        content = "content" +str(count)
        data[content] = text
        count=count+1
    yield data

【问题讨论】:

  • 分两步得到它 - 首先是bkz:,然后是mortingen,然后将它连接成一个字符串。
  • 我们无法运行代码,因此我们看不到它在单独的列中。为什么不使用将它放在一个列表中,然后将其全部加入一个列表中,然后将其放在一列中
  • 或者也许你应该首先获取所有.content而不使用::text,然后使用for-loop分别处理每个.content并仅在选定的.content中获取所有::text

标签: python web-scraping xpath scrapy css-selectors


【解决方案1】:

您应该首先获取所有.content 而不使用::text,然后使用for-loop 分别处理每个.content。对于每个.content,您应该运行::text 以仅获取此内容中的所有文本,放入列表中,然后将其加入单个字符串

       for count, content in enumerate(response.css('li .content')):
            text = []

            # get all `::text` in current `.content`
            for item in content.css('::text'):
                item = item.get()#.strip()
                # put on list
                text.append(item)

            # join all items in single string
            text = "".join(text)
            text = text.strip()

            print(count, '|', text)
            data[f"content {count}"] = text

最少的工作代码。

您可以将所有代码放在一个文件中并运行python script.py,而无需在scrapy 中创建项目。

import scrapy

class MySpider(scrapy.Spider):

    name = 'myspider'

    start_urls = ['https://eksisozluk.com/mortingen-sitraze--1277239']

    def parse(self, response):
        print('url:', response.url)

        data = {}  # PEP8: spaces around `=`

        title = response.css('[itemprop="name"]::text').get()
        data["title"] = title

        for count, content in enumerate(response.css('li .content')):
            text = []

            for item in content.css('::text'):
                item = item.get()#.strip()
                text.append(item)

            text = "".join(text)
            text = text.strip()

            print(count, '|', text)
            data[f"content {count}"] = text

        yield data
    
# --- run without project and save in `output.csv` ---

from scrapy.crawler import CrawlerProcess

c = CrawlerProcess({
    'USER_AGENT': 'Mozilla/5.0',
    # save in file CSV, JSON or XML
    'FEEDS': {'output.csv': {'format': 'csv'}},  # new in 2.1
})
c.crawl(MySpider)
c.start()

编辑:

getall() 稍微短一点

        for count, content in enumerate(response.css('li .content')):

            text = content.css('::text').getall()

            text = "".join(text)
            text = text.strip()

            print(count, '|', text)
            data[f"content {count}"] = text

【讨论】:

  • 非常感谢,它就像一个魅力
  • 嘿,你提到的代码运行良好,但我有一个问题?当页面上有一个段落时,它有
    所以段落之间有空格。刮痧时如何保存它们?目前换行符被忽略并加入段落
  • 在抓取之前,您必须将 <br> 替换为 '\n" - Preserving line breaks when parsing with Scrapy in Python
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 2011-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多