Handling pagination in lxml

此生再无相见时 提交于 2019-12-11 06:15:32

问题


I am trying to mirror a ruby scraper that I wrote but for a python only environment. I've decided to use lxml and requests to get this done. My problem is pagination:

base_url = "http://example.com/something/?page=%s"  
for url in [base_url % i for i in xrange(10)]:  
    r = requests.get(url)

I'm new to python and this library so I'm not sure the best way to perform the equivalent ruby code:

last_pg = (page.xpath("//div[contains(@class, 'b-tabs-utility')]").text.split('of ')[-1].split(' Results')[0].to_f / 60).ceil
puts "Current Index: #{last_pg}"
for pg_number in 1..last_pg do
  puts "Getting links on page #{pg_number}"

回答1:


Get the number of results by splitting the b-tabs-utility div into spaces and getting element before last:

base_url = "http://example.com/something/?page=%d" 
results_per_page = 60

div = page.xpath("//div[contains(@class, 'b-tabs-utility')]")[0].text
last_pg = int(div.split()[-2]) / results_per_page
for i in xrange(last_pg):
    r = requests.get(base_url % i)

I'm assuming the div text is in the following format, example: ... of 642 Results



来源:https://stackoverflow.com/questions/23318476/handling-pagination-in-lxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!