[英]Why is xpath's extract() returning an empty list for the href attribute of an anchor element?
该站点进行API调用以检索所有数据。 您可以使用scrapy shell查看该站点返回的响应。 scrapy shell 'https://www.udemy.com/courses/search/?src=ukw&q=accounting'
然后view(response)
。
您正在寻找的数据可通过以下api电话获得:' https ://www.udemy.com/api-2.0/search-courses/?fields[locale] = simple_english_title & src = ukw & q =accounting '。 但是,如果您尝试直接访问此链接,您将获得一个json对象,表示您无权执行此操作。 我是如何找到这个链接的? 在浏览器中加载URL,然后转到开发人员工具上的网络选项卡并查找XHR对象。
以下蜘蛛将首先向主链接发出请求,然后向api调用发出请求。 您必须解析返回的json对象以获取数据。 如果您想为更多产品扩展此蜘蛛,您可能希望在api调用的结构中查找模式。
import scrapy
class UdemySpider(scrapy.Spider):
name = 'udemy'
newurl = 'https://www.udemy.com/api-2.0/search-courses/?fields[locale]=simple_english_title&src=ukw&q=accounting'
def start_requests(self):
urls = ['https://www.udemy.com/courses/search/?src=ukw&q=accounting'
]
for url in urls:
yield scrapy.Request(url=url, callback=self.api_call)
def api_call(self, response):
print("Working on second page")
yield scrapy.Request(url=self.newurl, callback=self.parse)
def parse(self, response):
#code to parse json object
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.