请求 - 从基于api的网站获取数据

时间:2016-09-26 12:36:20

标签: python python-2.7 api python-requests

我想从this site获得所有评论。

首先,我使用此代码:

    <script type="text/javascript" src="js/EBloader.js"></script>
    <script type="text/javascript">
    function initEB(){
     if (!EB.isInitialized()) {
       EB.addEventListener(EBG.EventName.EB_INITIALIZED, startAdDS);
     }
     else {
       startAdDS();
     }
    }

    function startAdDS() {
      document.getElementById("banner").style.visibility = "visible";
    }

    function handleClickthroughButtonClick() {
      EB.clickthrough();
    }
   </script>

但是这样,我只能从第一页获得评论。

有人说我可以使用相同的[HttpPost] public ActionResult Index(UnauthenticatedEnquiryViewModel unauthenticatedEnquiryViewModel) { //unauthenticatedEnquiryViewModel.EnquiryType; //NEED TO ADD A METHOD THAT SENDS FILE TO CRM HERE if (ModelState.IsValid) { if (1 == 0) //take off { string fromAddress = WebConfigurationManager.AppSettings["FromEmail"]; //gets email from web.config file //var toAddress = new MailAddress(); //need to get this from crm var enquiry = DataAccessEnquiry.GetEnquiryCategoryEmail(unauthenticatedEnquiryViewModel.EnquiryType); //gets the to address based on CRm string UnauthEmailSubject = WebConfigurationManager.AppSettings["UnauthEmailSubject"]; //gets subject from web.config file MailMessage mailMessage = new MailMessage(fromAddress, enquiry.Email); //put to address frrom variable declared above mailMessage.Subject = UnauthEmailSubject; StringBuilder mailbuilder = new StringBuilder(); mailbuilder.AppendLine("First name: " + unauthenticatedEnquiryViewModel.FirstName); mailbuilder.AppendLine("Last name: " + unauthenticatedEnquiryViewModel.LastName); mailbuilder.AppendLine("Communication: " + unauthenticatedEnquiryViewModel.CCommmunication); mailbuilder.AppendLine("Email: " + unauthenticatedEnquiryViewModel.Email); mailbuilder.AppendLine("Confiirmation of email: " + unauthenticatedEnquiryViewModel.ConfirmEmailAddress); mailbuilder.AppendLine("Mobile telephone No: " + unauthenticatedEnquiryViewModel.MobileTelephoneNo); mailbuilder.AppendLine("Confiirmation of mobile telephone no: " + unauthenticatedEnquiryViewModel.ConfirmMobileTelephoneNo); mailbuilder.AppendLine("Alternative telephone no: " + unauthenticatedEnquiryViewModel.AlternativeTelephoneNo); mailbuilder.AppendLine("Confiirmation of alternative telephone no: " + unauthenticatedEnquiryViewModel.ConfirmAlternativeTelephoneNo); mailbuilder.AppendLine("I am a: " + unauthenticatedEnquiryViewModel.Profession); mailbuilder.AppendLine("Enquiry Type: " + unauthenticatedEnquiryViewModel.EnquiryType); mailbuilder.AppendLine("Your message: " + unauthenticatedEnquiryViewModel.YourMessage); if (unauthenticatedEnquiryViewModel.File != null) // this finds overall null { foreach (var file in unauthenticatedEnquiryViewModel.File) // loop through every File { if (file != null) //Finds induvidual null { var extension = new FileInfo(file.FileName).Extension.ToUpper(); mailMessage.Attachments.Add(new Attachment(file.InputStream, file.FileName)); } } } mailMessage.Body = mailbuilder.ToString(); SmtpClient smtpClient = new SmtpClient(); smtpClient.Send(mailMessage); } return View("Unauthsuccess", unauthenticatedEnquiryViewModel); } unauthenticatedEnquiryViewModel.Professions = DataAccessEnquiry.GetProfessionUnauthenticated(); unauthenticatedEnquiryViewModel.EnquiryTypes = new List<EnquiryType>(); } 模块来使用api。我发现api是https://api.traveloka.com/v1/hotel/hotelReviewAggregate,但我无法读取参数,因为我不知道如何使用select a.* from yourTable as a left join yourTable as b on a.ExpenseId = b.ExpenseID and a.Amount = -b.Amount where b.ExpenseID is not null 方式的api。

因此,我希望使用python或api参数获取所有评论的代码,以便在所有或特定页面中查看特定酒店。

1 个答案:

答案 0 :(得分:-1)

查看网络选项卡上的请求有效负载。有一个部分skip:8top:8,当您点击右箭头以获得下一页评论时,您会看到这些数字增加8。

您可以复制该请求并以相同方式删除结果

修改

使用chrome打开您的页面并点击f12。转到Network标签,向下滚动页面底部,即可进入下一批评论。只要您点击右箭头,就会填充网络选项卡。找到第二个hotelReviewAggregate并单击它。在标题标签下,您会看到Request Payload。打开data词典,找到skiptop。推进下一批评论,看看这些数字是如何变化的。您可以模拟此行为以访问其他页面。

然后,您需要做的是准备有效负载,您可以在其中增加值并发出GET个请求,并使用response objects使用BeautifulSoup抓取数据。

请求here

教程中的快速示例:

payload = {'key1': 'value1', 'key2': 'value2'} r = requests.get('http://httpbin.org/get', params=payload)

我不知道为什么人们在没有解释的情况下决定对我的答案给出负面价值。但是,哦,如果你觉得这很有用并回答你的问题,请接受它。