Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 重复行_Python 3.x_Xpath_Web Scraping_Scrapy - Fatal编程技术网

Python 3.x 重复行

Python 3.x 重复行,python-3.x,xpath,web-scraping,scrapy,Python 3.x,Xpath,Web Scraping,Scrapy,我正在努力浏览这个网站。 特别是,我正在努力浏览网站上的3个表格 我和你一起解决了这个问题 tables = response.xpath('//*[@class="table table-stripefd"]') 然后我想得到表格的每一行,我用 rows = tables.xpath('//tr') 这里的问题是,在抓取并打印出一些数据之后,我注意到有些行有多个条目。 例如,“Tahko vuorijuoksu”事件曾出现在网站上,但在我收集的数据中,我有3个实例 有人

我正在努力浏览这个网站。 特别是,我正在努力浏览网站上的3个表格

我和你一起解决了这个问题

tables = response.xpath('//*[@class="table table-stripefd"]')
然后我想得到表格的每一行,我用

rows = tables.xpath('//tr')
这里的问题是,在抓取并打印出一些数据之后,我注意到有些行有多个条目。 例如,“Tahko vuorijuoksu”事件曾出现在网站上,但在我收集的数据中,我有3个实例


有人能指出发生这种情况的原因吗?

当您像这样使用选择器时:

rows = tables.xpath('//tr')
rows = tables.xpath('.//tr') # notice the .
for table in tables:
    rows = table.xpath('tr')
它将选择其自身或子代轴中的每个
tr
元素,不受父元素的限制。因此,对于3个
元素中的每一个,它将返回所有207个
tr
元素

要仅获取每个表的
tr
元素child,您可以这样使用它:

rows = tables.xpath('//tr')
rows = tables.xpath('.//tr') # notice the .
for table in tables:
    rows = table.xpath('tr')
通常,这样写更直观:

rows = tables.xpath('//tr')
rows = tables.xpath('.//tr') # notice the .
for table in tables:
    rows = table.xpath('tr')

不过,这只是一个建议,前面的解决方案效果很好。

啊,我想这将是一个非常简单的错误。也许有一天我能真正理解xpath