我的 Python Scrapy 无法抓取 "keyword" 内容

My Python Scrapy cannot scrape out the "keyword" content

我无法抓取 "keyword" 内容。 >"< 我试了很多方法还是不行。

我已成功检索到其他内容,但仍然无法获取 "keyword" 内容。

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

关键词内容图片:

我的代码:

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

您使用 BeautifulSoup 而不是 scrapy select 有什么特别的原因吗?您的方法收到的响应已经作为一个 scrapy selector 可以同时执行 xpath 和 css selections.

table 中似乎有 3 个关键字。您可以 select 使用 xpath 或 css select 或者:

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']