用div刮取特定的表,用scrapy保存文本

2024-10-03 21:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用scrapy从一个表中的网站抓取内容。在

代码示例:

            <tr>
                <td><div>2018/2058</div></td>
                <td class="address"><div>Land North of 37 and 39 Hare Lane Claygate Esher Surrey KT10 9BT</div></td>
                <td class="proposal"><div>Confirmation of Compliance with Conditions: 5 (Tree Protection and Pre-Commencement Inspection) and 6 (Tree Protection) of planning permission 2017/0451.</div></td>
                <td><div style="min-width:90px">Claygate Ward</div></td>
            </tr>

但是,正如您可以看到的那样,文本位于每个“tr”标记的div中,如何使用xpath或css选择器获取文本?在

我试过了

^{pr2}$

以下是网站:

http://emaps.elmbridge.gov.uk/ebc_planning.aspx?requesttype=parsetemplate&template=WeeklyListAVDetailTab.tmplt&basepage=ebc_planning.aspx&Filter=^id^=%271%27&history=8a016b5504894a589b75179582da69ca&todatetext:PARAM=06%20July%202018&count:PARAM=63&id:PARAM=1&pagerecs=500&maxrecs=500

提前谢谢!在


Tags: andof文本divtreeparam网站tr
3条回答

使用gangabass中的xpath:

import scrapy

class txt_filter:
     txt= '<tr>\
                     <td><div>2018/2058</div></td>\
                     <td class="address"><div>Land North of 37 and 39 Hare Lane Claygate Esher Surrey KT10 9BT</div></td>\
                     <td class="proposal"><div>Confirmation of Compliance with Conditions: 6 (Tree Protection and Pre-Commencement Inspection) and 6 (Tree Protection) of planning permission 2017/0451.</div></td>\
                     <td><div style="min-width:90px">Claygate Ward</div></td>\
                </tr>'
     resp = scrapy.http.response.text.TextResponse(body=txt,url='abc',encoding='utf-8')
     print(resp.xpath('//tr[1]/td/div/text()').extract())

只从td中删除了[1]以获取所有行。在

你可以很容易地使用熊猫。在

table = pd.read_html(url)

现在表是一个包含完整表的数据帧

first_td_text = response.xpath('//tr[1]/td[1]/div/text()').extract_first()

更新

^{pr2}$

相关问题 更多 >