2016-10-21 113 views
-1

我要抓取所有这些结果在页面: http://www.carnival.com.au/Find-A-Cruise/search-results.aspx?ShipCode=LE&抓取某个网页与Python

的问题是,没有任何选项来显示他们。到目前为止,我成功地抓取了初始页面,但是我无法进入其他页面。如何完成这项工作?

+2

您使用了哪些工具?你能提供你到目前为止的代码吗? – sytech

+1

查看按钮上的单击事件处理程序 - 它发布到端点('http://www.carnival.com.au/DomainData/SailingSearch/Get/')并接收JSON作为响应。 –

回答

2

后续页面用JavaScript分页加载。您看到该请求正在发送到"http://www.carnival.com.au/DomainData/SailingSearch/Get/",并且POST请求中有一些参数。如果您嘲笑相同的请求,则会返回包含巡航信息的JSON数据。

import requests 
sesh = requests.Session() 
first_page = sesh.get("http://www.carnival.com.au/Find-A-Cruise/search-results.aspx?ShipCode=LE&#UBSELBWf2tB4Rs1H.97") 
data = {"ShipCode": "LE", "CurrencyCode": "AUD", "PageSize": 5, "PageNumber": 2, "SortExpression": "FirstSailDate"} 
page_2 = sesh.post("http://www.carnival.com.au/DomainData/SailingSearch/Get/", data=data) 
cruise_data = page_2.json() 

JSON响应甚至还跟显示有多少总成绩也有,你可以用更有效地请求后续页面。

该JSON的一些示例输出。

{'CurrentPage': '2', 
'CurrentResultsCount': '6 - 10', 
'LastPage': '9', 
'SortExpression': 'FirstSailDate', 
'TotalResultsCount': 44, 
'Voyages': [{'BookNowUrl': 'http://booking.carnival.com.au/index.asp?AIID=44&overridePageID=651&currentPageID=650&processingObjectIDList=21604&search 
Mode=searchByNumber&searchByNumberCriteria=G639&searchByCriteriaStatus=go&voyageCode=G639&voyageName=G639&shipCode=LE&shipName=Legend&brandCode=CL&bra 
ndName=Carnival%20Cruise%20Lines&homeCityCode=SYD&airCityCode=SYD&homeCityName=Sydney&airCityNameSydney&tDef=&tourName=&duration=10&embarkDate=2016121 
7&tType=O&tDirection=R&destinationCode=I&destinationName=Pacific+Islands&cruiseSelected=yes&unbundling=-&switchPolarRegion=prd&currencyCode=AUD', 
       'CruiseCode': 'G639 ', 
       'CruiseNights': 10, 
       'DateRangeText': '17 Dec 2016 (Sat - Tue)', 
       'DeparturePortCode': 'SYD', 
       'DeparturePortName': 'Sydney', 
       'FromBPrice': '1,699.00 AUD', 
       'FromIPrice': '1,549.00 AUD', 
       'FromOPrice': '1,649.00 AUD', 
       'FromQuadPrice': '1,689.00 AUD', 
       'FromSPrice': '2,649.00 AUD', 
       'FromTwinPrice': '1,549.00 AUD', 
       'MetaCategory': 'P', 
       'MetaCategoryDescription': 'Pacific Islands', 
       'PortsVisited': [{'CruiseDay': 0, 
           'PortCode': 'SYD', 
           'PortName': 'Sydney'}, 
           {'CruiseDay': 1, 
           'PortCode': 'NOU', 
           'PortName': 'Noumea'}, 
           {'CruiseDay': 2, 
           'PortCode': 'MY2', 
           'PortName': 'Mystery Island'}, 
           {'CruiseDay': 3, 
           'PortCode': 'LIF', 
           'PortName': 'Lifou Isle'}, 
           {'CruiseDay': 4, 
           'PortCode': 'MEE', 
           'PortName': 'Mare'}, 
           {'CruiseDay': 5, 
           'PortCode': 'SYD', 
           'PortName': 'Sydney'}], 
       'RegionCode': 'I', 
       'RegionName': 'Pacific Islands', 
       'SailDate': '/Date(1481950800000)/', 
       'ShipCode': 'LE', 
       'ShipName': 'Legend', 
+0

哦,我的上帝,我感到非常愚蠢......非常感谢。我会在下班后检查并标记答案。 – nephilimrising

+0

像一个魅力工作。我甚至可以输入页面大小100和页码1,并且一次获得所有结果 – nephilimrising