I've tried many different xpath expressions but can't quite get this to work. Basically, I will get the text for the first 10 rows in a table, then 'None' for the following 90. If I do a different xpath expression (one that was suggested to me but I personally don't fully understand), it gives me the last 90 but not the first 10. At first I thought it was due to tbody, so I removed that from my xpath expression. The class for the tag I am targeting also changes after the 10th row so I'm not sure if that makes a difference. I've tried inserting an "or" statement to try to get the best of both xpaths but it seems it has been improperly placed. Please help!
class CryptocurrenciesSpider(scrapy.Spider):
name = 'cryptocurrencies'
allowed_domains = ['www.coinmarketcap.com']
def start_requests(self):
yield scrapy.Request(url='https://www.coinmarketcap.com/', callback=self.parse)
#Udemy Answer. Yields last 90 with 'None' for the first 10 rows.
def parse(self, response):
for row in response.xpath("//table//tr"):
currency = row.xpath(".//td/a/span[2]/text()").get()
if currency:
yield {
'currency': currency
}
#Yields the first 10 and then 'None' for the rest.
def parse(self, response):
for row in response.xpath(".//table//tr"):
yield {
'Currency': row.xpath(".//td[3]/div/a/div/div/p/text()").get()
}
# Tried the "or" operator to no avail.
# row.xpath(".//td[3]/div/a/div/div/p/text() or .//td/a/span[2]/text()").get(
# row.xpath(".//td[3]/div/a/div/div/p/text()" or ".//td/a/span[2]/text()").get()