【问题标题】:Scrapy get text out of spanScrapy使文本超出范围
【发布时间】:2019-04-13 21:09:14
【问题描述】:

网址:https://myanimelist.net/anime/236/Es_Otherwise

我试图在 URL 中抓取以下内容:

我试过了:

for i in response.css('span[class = dark_text]') :
    i.xpath('/following-sibling::text()')

或者当前的 XPath 不工作或者我错过了什么......

aired_xpath = response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[11]/text()')

producer_xpath = response.xpath("//*[@id='content']/table/tbody/tr/td[1]/div/div[12]/span/a/@href/text()")
licensor_xpath = response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[13]/a/text()')
studio_xpath response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[14]/a/@href/title/text()')
studio_xpath = response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[17]/text()')
str_rating_xpath = response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[18]/text()')
ranked_xpath = response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[20]/span/text()')
japanese_title_xpath = response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[7]/text()')
source_xpath = response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[15]/text()')
genre_xpath = [response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[16]/a[{0}]'.format(i)) for i in range(1,4)]
genre_xpath_v2 = response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[16]/a/@href/text()')
number_of_users_rated_anime_xpath = response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[19]/span[3]/text()')
popularity_xpath = response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[21]/span/text()')
members_xpath = response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[22]/span/text()')
favorite_xpath =  response.xpath('//*[@id="content"]/table/tbody/tr/td[1]/div/div[23]/span/text()')

但我发现某些文本超出了跨度类,因此我想使用 css/XPath 公式使该文本超出跨度。

【问题讨论】:

  • 嗨。请你写一段左右来更好地解释你的问题?
  • 您想使用什么语言?您是否与该网站达成协议以抓取内容?
  • 我使用 python 和 scrapy 框架

标签: python html css scrapy


【解决方案1】:

如果你只是想废弃你在图片中提到的信息,你可以利用

response.xpath('//div[@class="space-it"]//text()').extract()

或者我无法正确理解您的问题。

【讨论】:

  • 以下语法返回空列表
  • 您是否更改了班级名称?实际上类名是 spaceit
  • 为了更好的结果你可以试试 response.xpath('//div[@class="js-scrollfix-bottom"]//div[@class="spaceit"]
  • 只是它不会返回您的替代名称和类型
【解决方案2】:

在表格内循环遍历 div 更简单

foundH2 = False
response =  Selector(text=htmlString).xpath('//*[@id="content"]/table/tr/td[1]/div/*')

for resp in response:
  tagName = resp.xpath('name()').extract_first()
  if 'h2' == tagName:
    foundH2 = True
  if foundH2:
    # start adding 'info' after <h2>Alternative Titles</h2> found
    info = None
    if 'div' == tagName:
      for item in resp.xpath('.//text()').extract():
        if 'googletag.' in item: break
        item = item.strip()
        if item and item != ',':
          info = info + " " + item if info else item
      if info:
        print info

只是我的看法,beautifulSoup 比 scrapy 更快更好。

【讨论】:

  • 谢谢它的工作,但什么是名称和 googletag ?你能解释一下你的代码吗?
  • div内容在Favorites: 27之后,找到后停止循环
猜你喜欢
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多