python-3.x - 如何使用 scrapy 从该 url 刮取多个表格行?
问题描述
import scrapy
class SsoSpider(scrapy.Spider):
name = 'sso'
allowed_domains = ['www.sso.agc.gov.sg']
start_urls = ['https://sso.agc.gov.sg/Browse/Act/Current']
def parse(self, response):
acts = response.xpath("//table[@class='table browse-list']/tbody")
for act in acts:
yield {
#'Act title': act.xpath(".//tr[@class='alternate']/td/a/text()").get(),
'Act title': act.xpath(".//tr/td/a/text()").get(),
#'Short-hand code': act.xpath(".//tr[@class='alternate']/td/a/@href").get()
'Short-hand code': act.xpath(".//tr/td/a/@href").get()
}
所以这是我上面用于抓取的代码。运行后,我只得到 1 个抓取结果。
我是新手,所以任何帮助将不胜感激!
解决方案
试试这个,我希望它会工作。
import scrapy
class SsoSpider(scrapy.Spider):
name = 'sso'
allowed_domains = ['www.sso.agc.gov.sg']
start_urls = ['https://sso.agc.gov.sg/Browse/Act/Current']
def parse(self, response):
acts = response.xpath("//*[@id='listPanel']/table/tbody/tr")
for act in acts:
yield {
'Act title': act.xpath("td[1]/a/text()").extract_first(),
'Short-hand code': act.xpath("td/a/@href").extract_first()
}
让我知道。
推荐阅读
- python-3.x - 如何在谷歌云控制台中为云功能设置日志文件?
- javascript - 如何确定出现在 Stripe 结帐中的产品的顺序/顺序
- amazon-web-services - 从手动批准步骤将输入传递给 Step Function
- python - RuntimeError: 给定组=1,大小为 [64, 1, 4, 4] 的权重,预期输入 [256, 3, 32, 32] 有 1 个通道,但有 3 个通道
- c# - 在 Azure Functions 中的何处存储共享点路径?
- c++ - 我应该如何在 C/C++ 中计算 GF(2) 上的矩形稀疏矩阵的零空间?
- python - SciPy 最小化算法的比对
- c# - 在 C# 中使用正则表达式查找字符串并用左字符串替换
- docker - Graylog 字段“源”:FQDN 而不是带有 Serilog 的 DockerId
- python - 如何通过写入“/dev/input/EventX”在一个文件流中发送和执行多个事件