Python 从内部页面获取数据并与当前页面合并
在我的html页面中有两列表格,第一列是名称,第二列是有日期的链接,我希望能够下载此页面,获取此日期并提高它,因此在输出中我将有名称和日期。 例如 在第一页我们有Python 从内部页面获取数据并与当前页面合并,python,web-crawler,scrapy,Python,Web Crawler,Scrapy,在我的html页面中有两列表格,第一列是名称,第二列是有日期的链接,我希望能够下载此页面,获取此日期并提高它,因此在输出中我将有名称和日期。 例如 在第一页我们有 <table> <tr> <td>A</td> <td>http://something.com/2564.html</td> </tr> </table> 在2564.html
<table>
<tr>
<td>A</td>
<td>http://something.com/2564.html</td>
</tr>
</table>
在2564.html页面中有
<body>
<p>the date is: 25 April 2009</p>
</body>
我怎么能有
<xml>
<row>
<name>A</name>
<date>25 April 2009</date>
</row>
</xml>
我的方法是创建项目,用我在这个页面上的数据填充它,然后生成一个缺少数据的页面请求,在meta中传递项目。下载第二页时,我从meta获取项目,并填充其他数据:
def parseItem(self, response):
'''Get date from the first page.'''
item = Item()
item['firstdata'] = '???'
...
otherDataPageLink = '???'
yield Request(otherDataPageLink, meta = {'item': item}, callback = self.parseComments)
def parseComments(self, response):
'''Get all the other data from second page.'''
item = response.meta['item']
item['otherdata'] = '???'
yield item # return the item with all the data
加载此页面时,您是否获得了所需的所有DOM元素。如果答案是肯定的,您可以使用sgml解析器,我将与您分享一些示例代码