我想要刮下相关网站链接共享下面。我需要一些参数,我找到了这样的最佳解决方案对我来说。但我需要scape不同的2部分,我不知道如何很好地结合它(结合作为列)这就是为什么我需要你的帮助。我也愿意接受更好的解决方案。我还需要跳过一些错误的scrape行。也不想添加一些空行。我将输出作为文件共享。 http://s7.dosya.tc/server14/tnx4u0/test.json.zip.html
实际上它必须是基本循环内的表循环。但是为了展示它我现在做得更好。非常感谢
class KingsatSpider(Spider):
name = 'kingsat'
allowed_domains = ['https://tr.kingofsat.net/tvsat-turksat4a.php']
start_urls = ['https://tr.kingofsat.net/tvsat-turksat4a.php']
def parse(self, response):
tables=response.xpath('//*[@class="fl"]/tr')
bases=response.xpath('//table[@class="frq"]/tr')
for base in bases:
yield {
'Frekans':base.xpath('.//td[3]/text()').extract_first(),
'Polarizasyon':base.xpath('.//td[4]/text()').extract_first(),
'Kapsam':base.xpath('.//td[6]/a/text()').extract_first(),
'SR':base.xpath('.//td[9]/a[1]/text()').extract_first(),
'FEC':base.xpath('.//td[9]/a[2]/text()').extract_first(),
}
for table in tables:
yield {
'channel' :table.xpath('.//td[3]/a/text()').extract_first(),
'V-PID' : table.xpath('.//td[9]/text()[1]').extract_first(),
'A-PID' : table.xpath('.//td[10]/text()[1]').extract_first(),
}
页面有建设
等等
你得到bases
中的所有标题和tables
中的所有行作为单独的项目,但你必须将表格作为单个元素,这样你就可以创建对(基数,表格),然后你应该从每个表中得到行并用正确的base
得到
在xpath我得到没有tables
的tr
- 所以我可以创建对(base
,table-with-all-its-rows
)。
然后我可以从table
获得行,并用base
收益。
我无法测试它。也许你必须先跳过base
- zip(bases[1:], tables)
bases = response.xpath('//table[@class="frq"]/tr')
tables = response.xpath('//*[@class="fl"]')
for base, tabel in zip(bases, tables):
rows = table.xpath('.//tr')
for row in rows:
yield {
'Frekans': base.xpath('.//td[3]/text()').extract_first(),
'Polarizasyon': base.xpath('.//td[4]/text()').extract_first(),
'Kapsam': base.xpath('.//td[6]/a/text()').extract_first(),
'SR': base.xpath('.//td[9]/a[1]/text()').extract_first(),
'FEC': base.xpath('.//td[9]/a[2]/text()').extract_first(),
'channel' : row.xpath('.//td[3]/a/text()').extract_first(),
'V-PID' : row.xpath('.//td[9]/text()[1]').extract_first(),
'A-PID' : row.xpath('.//td[10]/text()[1]').extract_first(),
}
如果表格与基数相关,你可以不需要将它们分成两部分,这是最好的解决方法。如果它们彼此不相关且它们的数量相同,则可以使用以下方法。
def parse(self, response):
tables=response.xpath('//*[@class="fl"]/tr')
bases=response.xpath('//table[@class="frq"]/tr')
for i in range(len(bases)):
yield {
'Frekans':base[i].xpath('.//td[3]/text()').extract_first(),
'A-PID' : table[i].xpath('.//td[10]/text()[1]').extract_first(),
}
如果它们的数量不一样,你只能将它们视为整体。那么你可以在管道中处理它