我用一个ajax的网站来刮刮我的内容。 首先,由于“csrf”的原因,我不能模拟ajax请求。 主要的问题是,这里是我想搜集的链接:
https://lastsecond.ir/hotels
此网站url的模式如下:
^{pr2}$ajax产生的内容,所以我不得不使用selenium在浏览器中稍等片刻,但我不能移动页面并获取所有内容!我只是得到第一页的内容,我没有得到任何错误。 这是我的蜘蛛代码:
class HotelsSpider(CrawlSpider):
name = 'hotels'
allowed_domains = ['lastsecond.ir']
start_urls = ['http://lastsecond.ir/hotels']
rules = (
Rule(LinkExtractor(allow=r'/hotels\?page=[0-9]/'),
callback='parse_item', follow=True),
)
def __init__(self, *args, **kwargs):
super(HotelsSpider, self).__init__(*args, **kwargs)
self.driver = webdriver.Chrome(executable_path='chromedriver.exe')
def parse_item(self, response):
self.driver.get("http://lastsecond.ir/hotels?page=1")
WebDriverWait(self.driver, 30).until(
EC.presence_of_element_located((By.ID, "panel1"))
)
response = TextResponse(url=response.url,
body=self.driver.page_source, encoding='utf-8')
hotel = ItemLoader(item=HotelItem(), response=response)
hotel.add_css('hotel_name', '#panel1 h2.semimedium-font-size
a::text')
return hotel.load_item()
每个页面都有一个ajax请求,并且没有“next”链接,也没有编号的分页。 我的主要问题是我只得到第一页的内容!在
Token
位于http://lastsecond.ir/hotels
中的JavaScript
代码中,即所以可以使用标准字符串函数来获取它。在
如果您有令牌,那么您可以使用
FormRequest()
创建对POST
的POST
请求,而不需要Selenium
在
^{pr2}$FormRequest()
中使用dont_filter=True
,因为它将多次执行同一个url,而scrapy通常会跳过重复的url。在部分结果显示在屏幕上。{所有数据都保存在^中。在
相关问题 更多 >
编程相关推荐