小白教程

 找回密码
 立即注册
查看: 5610|回复: 2

[已解决]网站翻页失败

[复制链接]

1

主题

1

帖子

3

积分

新手上路

Rank: 1

积分
3
发表于 2021-3-9 17:16:27 | 显示全部楼层 |阅读模式
爬行https://www.turners.co.nz/Cars/Used-Cars-for-Sale 网站上的图片数据在翻页时出现问题。无论设置多少页,都只返回第一页的结果。代码如下
  1. # -*- coding:utf8 -*-
  2. import requests
  3. from bs4 import BeautifulSoup

  4. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}
  5. def RequestWithPageno(pageno=1):
  6.     form_data = {
  7.         'sortorder': 0,
  8.         'pagesize': 24,
  9.         'pageno': pageno}
  10.     # url = 'https://www.turners.co.nz/Cars/Used-Cars-for-Sale/?sortorder=0&pagesize=24&pageno='+str(pageno)
  11.     response = requests.get(url, headers=headers, data=form_data)
  12.     soup = BeautifulSoup(response.text, 'html.parser')
  13.     divs = soup.select('#searchResultsContainer > div')
  14.     for div in divs:
  15.         #显示第一个车辆的信息即可
  16.         goodnumber = div.get("data-goodnumber")
  17.         print(goodnumber)
  18.         break
  19. # url = 'https://www.turners.co.nz/Cars/Used-Cars-for-Sale/?sortorder=0&pagesize=24&pageno=1'
  20. url = 'https://www.turners.co.nz/Cars/Used-Cars-for-Sale'
  21. if __name__ == "__main__":
  22.     pageno = 1
  23.     while True:
  24.         print(pageno)
  25.         RequestWithPageno(pageno=pageno)
  26.         pageno += 1
复制代码


最佳答案
2021-3-13 00:48:36
其实请求的不是https://www.turners.co.nz/Cars/U ... ize=24&pageno=2这个地址实际上是https://www.turners.co.nz/Client/car/SearchList这个地址。
  1. post 的data
  2. {
  3.     "pageno": 3,
  4.     "filters": {
  5.         "sortorder": "0",
  6.         "pagesize": "24",
  7.         "pageno": 3,
  8.         "searchfor": null,
  9.         "trans": null,
  10.         "drivetype": null,
  11.         "featurestechnology": null,
  12.         "featuresinterior": null,
  13.         "featuresperformance": null,
  14.         "featuresother": null,
  15.         "financefrom": null,
  16.         "financeto": null,
  17.         "fuelefficiencyrating": null,
  18.         "safetyrating": null,
  19.         "hasonroadcosts": null,
  20.         "isnznew": null,
  21.         "hasnoreserve": null,
  22.         "hascga": null,
  23.         "isdiscounted": null,
  24.         "iscertified": null,
  25.         "fuels": null,
  26.         "seats": null,
  27.         "colours": null,
  28.         "types": null,
  29.         "bodystyles": null,
  30.         "make": null,
  31.         "models": null,
  32.         "locations": null,
  33.         "industry": null,
  34.         "category": null,
  35.         "subcategories": null,
  36.         "yearfrom": null,
  37.         "yearto": null,
  38.         "odofrom": null,
  39.         "odoto": null,
  40.         "pricefrom": null,
  41.         "priceto": null,
  42.         "enginefrom": null,
  43.         "engineto": null,
  44.         "salemethods": null,
  45.         "regstatus": null,
  46.         "custom": null
  47.     }
  48. }
复制代码
回复

使用道具 举报

0

主题

1

帖子

2

积分

新手上路

Rank: 1

积分
2
发表于 2021-3-13 00:48:36 | 显示全部楼层 &
其实请求的不是https://www.turners.co.nz/Cars/U ... ize=24&pageno=2这个地址实际上是https://www.turners.co.nz/Client/car/SearchList这个地址。
  1. post 的data
  2. {
  3.     "pageno": 3,
  4.     "filters": {
  5.         "sortorder": "0",
  6.         "pagesize": "24",
  7.         "pageno": 3,
  8.         "searchfor": null,
  9.         "trans": null,
  10.         "drivetype": null,
  11.         "featurestechnology": null,
  12.         "featuresinterior": null,
  13.         "featuresperformance": null,
  14.         "featuresother": null,
  15.         "financefrom": null,
  16.         "financeto": null,
  17.         "fuelefficiencyrating": null,
  18.         "safetyrating": null,
  19.         "hasonroadcosts": null,
  20.         "isnznew": null,
  21.         "hasnoreserve": null,
  22.         "hascga": null,
  23.         "isdiscounted": null,
  24.         "iscertified": null,
  25.         "fuels": null,
  26.         "seats": null,
  27.         "colours": null,
  28.         "types": null,
  29.         "bodystyles": null,
  30.         "make": null,
  31.         "models": null,
  32.         "locations": null,
  33.         "industry": null,
  34.         "category": null,
  35.         "subcategories": null,
  36.         "yearfrom": null,
  37.         "yearto": null,
  38.         "odofrom": null,
  39.         "odoto": null,
  40.         "pricefrom": null,
  41.         "priceto": null,
  42.         "enginefrom": null,
  43.         "engineto": null,
  44.         "salemethods": null,
  45.         "regstatus": null,
  46.         "custom": null
  47.     }
  48. }
复制代码
回复

使用道具 举报

0

主题

1

帖子

2

积分

新手上路

Rank: 1

积分
2
发表于 2021-4-4 01:00:47 | 显示全部楼层
  1. # -*- coding:utf8 -*-
  2. import requests
  3. from bs4 import BeautifulSoup
  4. import json
  5. import time
  6. headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'}
  7. def RequestWithPageno(pageno=1):
  8.     form_data = {
  9.     "pageno": pageno,
  10.     "filters": {
  11.         "sortorder": "0",
  12.         "pagesize": "24",
  13.         "pageno": pageno}
  14.     }
  15.     print(form_data)
  16.     url = 'https://www.turners.co.nz/Client/car/SearchList'
  17.     response = requests.post(url=url, headers=headers, data=form_data)
  18.     jsondata = json.loads(response.text)
  19.     print('totalResults:'+str(jsondata['totalResults']))
  20.     print('startIndex:'+str(jsondata['startIndex']))
  21.     print('endIndex:'+str(jsondata['endIndex']))
  22.     text = response.text.encode('utf-8').decode('unicode_escape')
  23.     # print(text)
  24.     soup = BeautifulSoup(text, 'html.parser')
  25.     # print(soup)
  26.     divs = soup.findAll('div', class_='car-summary-images')
  27.     # print(divs)
  28.     for div in divs:
  29.         # 显示第一个车辆的信息即可
  30.         print(div.a['href'])
  31.         print(div.a.img['alt'])
  32.         break
  33. # url = 'https://www.turners.co.nz/Cars/Used-Cars-for-Sale/?sortorder=0&pagesize=24&pageno=1'
  34. if __name__ == "__main__":
  35.     pageno = 5
  36.     while True:
  37.         RequestWithPageno(pageno=pageno)
  38.         time.sleep(60)
  39.         pageno += 1
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 14:37 , Processed in 0.144398 second(s), 25 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

快速回复 返回顶部 返回列表