I am using Scrapy to crawl a google results page:
在response.xpath('//*[@id="ires"]/ol/div/h3/a')
下,我想选择data-href
属性。
然而,当我尝试response.xpath('//*[@id="ires"]/ol/div/h3/a/@data-href')
我想问一下选择这个属性的正确xpath表达式是什么。提前谢谢。
答案 0 :(得分:0)
Scrapy
收到的HTML与您在浏览器中看到的不一样,Scrapy本身不是浏览器,没有内置JavaScript引擎。首先使用Scrapy Shell调试您的站点。
在这种情况下,您需要使用href
代替data-href
:
//*[@id="ires"]/ol/div/h3/a/@href
来自shell的演示:
$ scrapy shell https://www.google.com/search?q=anaheim%20pet%20friendly%20vacation%20rentals
>>> response.xpath('//*[@id="ires"]/ol/div/h3/a/@href').extract()
[u'/url?q=http://www.bringfido.com/lodging/rentals/city/anaheim_ca_us/&sa=U&ved=0ahUKEwjttJG3m9DKAhWDqh4KHeXpBvsQFggoMAA&usg=AFQjCNFFYOmY4s-kml5a23XklwzDTSGq1w', u'/url?q=https://www.flipkey.com/pet-friendly-anaheim-vacation-rentals/g29092/s/&sa=U&ved=0ahUKEwjttJG3m9DKAhWDqh4KHeXpBvsQFgguMAE&usg=AFQjCNHuXcfNgk-1JjL0xb8sl2V3AkiS8A', u'/url?q=http://www.tripadvisor.com/VacationRentals-g659482-Reviews-zfn1-Orange_County_California-Pet_Friendly_Vacation_Rentals.html&sa=U&ved=0ahUKEwjttJG3m9DKAhWDqh4KHeXpBvsQFgg0MAI&usg=AFQjCNG0n1JiCRpWthVuxL_b1ObwTW4JaA', u'/url?q=https://www.airbnb.com/explore/anaheim--ca/vacation-rentals/pet-friendly&sa=U&ved=0ahUKEwjttJG3m9DKAhWDqh4KHeXpBvsQFgg6MAM&usg=AFQjCNEDRLlfnPeWHxs5mGDzObFBZhmqBQ', u'/url?q=https://www.homeaway.com/vacation-rentals/california/anaheim/r6842&sa=U&ved=0ahUKEwjttJG3m9DKAhWDqh4KHeXpBvsQFgg_MAQ&usg=AFQjCNGcQ-Sxb3aPdoJZoxwU7vmAeYmVgA', u'/url?q=http://www.petvacationhomes.com/search/cityListing/st/California.html&sa=U&ved=0ahUKEwjttJG3m9DKAhWDqh4KHeXpBvsQFghIMAU&usg=AFQjCNG1XG-D5hyBAMfl12J-9P5VyEAOCg', u'/url?q=https://www.vacationhomerentals.com/California/Orange-County/Anaheim-vacation-rentals/g2769/&sa=U&ved=0ahUKEwjttJG3m9DKAhWDqh4KHeXpBvsQFghOMAY&usg=AFQjCNFV8DrK4dwWT0N9DiGEWZLo7n3PjA', u'/url?q=https://www.vacationhomerentals.com/California/Orange-County/Anaheim-pet-friendly-vacation-rentals/g2769/s/&sa=U&ved=0ahUKEwjttJG3m9DKAhWDqh4KHeXpBvsQFghTMAc&usg=AFQjCNEoTBZTYE7ydwWo2w3XHHUq3pcObQ', u'/url?q=https://www.vrbo.com/vacation-rentals/usa/california/orange-county/anaheim-area/anaheim&sa=U&ved=0ahUKEwjttJG3m9DKAhWDqh4KHeXpBvsQFghYMAg&usg=AFQjCNHi7YS2PmqTdMTYcv_u8NrblweE2g', u'/url?q=http://vacationrentals.travel.aol.com/listings/location-Anaheim--CA&sa=U&ved=0ahUKEwjttJG3m9DKAhWDqh4KHeXpBvsQFgheMAk&usg=AFQjCNFORzePpDlTr87Wn2gy4bjUxjf6mg']