我正试着用scrapy来抓取this website(有多个页面)。问题是我找不到下一页的url。
你对如何用多个页面(用scrapy)创建一个网站,或者如何解决我的代码出现的错误有什么想法吗?
我尝试了下面的代码,但它不起作用:
class AbcdspiderSpider(scrapy.Spider):
"""
Class docstring
"""
name = 'abcdspider'
allowed_domains = ['abcd-terroir.smartrezo.com']
alphabet = list(string.ascii_lowercase)
url = "https://abcd-terroir.smartrezo.com/n31-france/annuaireABCD.html?page=1&spe=1&anIDS=31&search="
start_urls = [url + letter for letter in alphabet]
main_url = "https://abcd-terroir.smartrezo.com/n31-france/"
crawl_datetime = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
start_time = datetime.datetime.now()
def parse(self, response):
self.crawler.stats.set_value("start_time", self.start_time)
try:
page = response.xpath('//div[@class="pageStuff"]/span/text()').get()
page_max = get_num_page(page)
for index in range(page_max):
producer_list = response.xpath('//div[@class="clearfix encart_ann"]/@onclick').getall()
for producer in producer_list:
link_producer = self.main_url + producer
yield scrapy.Request(url=link_producer, callback=self.parse_details)
next_page_url = "/annuaireABCD.html?page={}&spe=1&anIDS=31&search=".format(index)
if next_page_url is not None:
yield scrapy.Request(response.urljoin(self.main_url + next_page_url))
except Exception as e:
self.crawler.stats.set_value("error", e.args)
我得到这个错误:
'error': ('range() integer end argument expected, got unicode.',)
最佳答案
错误就在这里:
page = response.xpath('//div[@class="pageStuff"]/span/text()').get()
page_max = get_num_page(page)
range函数需要整数值(1、2、3、4等)而不是unicode字符串('page 1/403'
)
我对范围误差的建议是
page = response.xpath('//div[@class="pageStuff"]/span/text()').get().split('/ ')[1]
for index in range(int(page)):
#your actions
关于python - 如何使用scrapy抓取具有多个页面的网站,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56905178/