简体   繁体   中英

Scrape Table Using Scrapy

apologies for the long post -

I have a table that I am trying to dig into using scrapy, but can't quite figure out how to dig into this table deep enough.

This is the table:

<table class="detail-table" border="0" cellspacing="0">
 <tbody>
 <tr id="trAnimalID">
  ...
 </tr>
 <tr id="trSpecies">
  ...
 </tr>
 <tr id="trBreed">
  ...
 </tr>
 <tr id="trAge">
  ...
 <tr id="trSex">
  ...
 </tr>
 <tr id="trSize">
  ...
 </tr>
 <tr id="trColor">
  ...
 </tr>
 <tr id="trDeclawed">
  ...
 </tr>
 <tr id="trHousetrained">
  ...
 </tr>
 <tr id="trLocation">
  ...
 </tr>
 <tr id="trIntakeDate">
  <td class="detail-label" align="right">
   <b>Intake Date</b>
  </td>
  <td class="detail-value">
   <span id="lblIntakeDate">3/31/2020</span>&nbsp;
  </td>
 </tr>
 <tr id="trStage">
  <td class="detail-label" align="right">
   <b>Stage</b>
  </td>
  <td class="detail-value">
   <span id="lblStage">Reserved</span>
  </td>
 </tr>
 </tbody></table>

I can dig into it using the scrapy shell command:

text = response.xpath('//*[@class="detail-table"]//tr')[10].extract()

I am getting back this:

'<tr id="trIntakeDate">\r\n\t
  <td class="detail-label" align="right">\r\n
   <b>Intake Date</b>\r\n
  </td>\r\n\t
  <td class="detail-value">\r\n
   <span id="lblIntakeDate">3/31/2020</span>\xa0\r\n
  </td>\r\n
</tr>'

I can't quite figure out how to just get the value for lblIntakeDate. I just need 3/31/2020 . Additionally, I'd like to run this as a lambda, and can't quite figure out how to get the execute function to dump out a json file like I can using command line. Any ideas?

Try it:

//table[@class='detail-table']/tbody//tr/td/span[@id='lblIntakeDate']/text()

Go https://www.online-toolz.com/tools/xpath-tester-online.php And please remove redundant characters such as &nbsp;

xpath_online_tester

try:

from urllib.request import urlopen

url = ''
html = urlopen(url)
bs = BeautifulSoup(html.read(), 'html.parser')

for i in bs.find_all('a'):
    print(i.get_text())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM