我不能刮“关键字”的内容>;”&书信电报; 我试过很多方法,但还是失败了
我已成功检索其他内容,但仍然无法获取“关键字”内容
有人能帮忙修复这个错误吗?? 关键字内容位于“#关键字_表a”, 或XPath“/*[@id=“关键字表”]/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选择器有什么特别的原因吗?您的方法接收的响应已经充当了一个scrapy选择器,它可以进行xpath和css选择
表中似乎有3个关键字。您可以使用xpath或css选择器选择它们:
相关问题 更多 >
编程相关推荐