我正在尝试获取多个表,这些表的名称存储在h3标记下。有列的数据我可以抓取没有问题,当我输入下一个url时,我可以将这些数据附加到csv文件中。 我无法解决的问题是获取表头并将其相对于表的每一行进行存储。这样做的原因是当下一个表被送入时,我需要知道它属于哪个表。是否可以使用len循环来确定表的长度,然后将表头写入每一行?有可能出口商品吗?在
这是我的密码 蜘蛛网.py在
from bigcrawler.items import BigcrawlerItem
from scrapy import Spider, Request, Selector
from scrapy.selector import Selector
from bigcrawler.items import MatchStatItemLoader
class CrawlbotSpider(Spider):
name = 'bigcrawler'
allowed_domains = ['www.matchstat.com']
start_urls = [
'https://matchstat.com/tennis/tournaments/w/Taipei/2015',
'https://matchstat.com/tennis/tournaments/w/Hong%20Kong/2017',
]
def parse_header(self , response):
hxs = Selector(response)
for tb in hxs.css('tr.match'):
heading = tb.xpath('//*[@id="AWS"]/div/h3/text()').extract()[0]
for td in tb.xpath(".//tr[contains(@class,
'match')]/td[contains(@class, 'round')]/text()"):
il = BigcrawlerItem(selector=td)
il.add_value('event_title' , heading)
yield il.load_item()
def parse(self , response):
for row in response.css('tr.match'):
il = MatchStatItemLoader(selector=row)
il.add_css('round' , '.round::text')
il.add_css('event1' , '.event-name a::text')
il.add_css('player_1' , '.player-name:nth-child(2) a::text')
il.add_css('player_2' , '.player-name:nth-child(3) a::text')
il.add_css('player_1_odds' , '.odds-td.odds-0
[payout]::text')
il.add_css('player_2_odds' , '.odds-td.odds-1
[payout]::text')
il.add_css('h_2_h' , 'a.h2h::text')
yield il.load_item()
在项目.py在
^{pr2}$
我建议完全不要使用},因为它们确实令人困惑。请参阅此处的完整工作代码。还要注意
Items
类,而使用start_requests
方法而不是{match_heading
变量。在如果其中只有一个标题不需要相对于当前节点,请尝试以下操作:
但如果需要它相对于当前节点,也可以执行以下操作:
^{pr2}$相关问题 更多 >
编程相关推荐