我的Python Scrapy无法删除“关键字”内容

2024-09-29 19:30:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我不能刮“关键字”的内容>;”&书信电报; 我试过很多方法,但还是失败了

我已成功检索其他内容,但仍然无法获取“关键字”内容

有人能帮忙修复这个错误吗?? 关键字内容位于“#关键字_表a”, 或XPath“/*[@id=“关键字表”]/tbody/tr/td[2]/a”

关键词内容图片:

enter image description here

我的代码:

import scrapy 
from bs4 import BeautifulSoup
from digitimes.items import DigitimesItem


class digitimesCrawler(scrapy.Spider):
    name = 'digitimes'
    start_urls = ["http://www.digitimes.com.tw/tw/dt/n/shwnws.asp?id=435000"]


def parse(self, response):
    soup = BeautifulSoup(response.body,'html.parser')
    soupXml = BeautifulSoup(response.body, "lxml")
    simpleList = []

    item = DigitimesItem()

    timeSel=soup.select('.insubject .small')   
    tmpTime = timeSel[0].text
    time = tmpTime[:10]
    item['time'] = time #處理完時間啦
    print(time)

    titleSel = soup.select('title')
    title = titleSel[0].text
    item['title'] = title #處理完時間啦
    print(title)

    #================== To Resolve ==================

    keywordOutput="" 
    for k in soupXml.select('#keyword_table a'):
        for key in k:
            keywordOutput = keywordOutput + key + " "
    item['keyword'] = keywordOutput 
    print(keywordOutput)

    #================== To Resolve ==================



    categoryOutput=""
    for m in soup.select('#sitemaptable tr td a'):
        for cate in m:
            if(cate!="DIGITIMES"):
                categoryOutput = categoryOutput + cate + " "
    item['cate'] = categoryOutput  
    print(categoryOutput) 

    simpleList.append(item)
    return simpleList

Tags: inimport内容fortimetitle关键字item
1条回答
网友
1楼 · 发布于 2024-09-29 19:30:21

您使用BeautifulSoup而不是scrapy选择器有什么特别的原因吗?您的方法接收的响应已经充当了一个scrapy选择器,它可以进行xpath和css选择

表中似乎有3个关键字。您可以使用xpath或css选择器选择它们:

response.css("#keyword_table a::text").extract()
# or with xpath
response.xpath("//*[@id='keyword_table']//a/text()").extract()
# both return
>>> [u'Sony', u'\u5f71\u50cf\u611f\u6e2c\u5668', u'\u80a1\u7968\u4ea4\u6613']

相关问题 更多 >

    热门问题