【问题标题】:BeautifulSoup webscraping load more from ajax json dataBeautifulSoup webscraping 从 ajax json 数据加载更多
【发布时间】:2017-07-24 03:53:15
【问题描述】:

我想从这个页面抓取数据:https://www.landefeld.de/gruppe/en/straight-screwed-connection-metric-/GE4LLM

我关注了一些类似这样的主题:BeautifulSoup subpages of list with "load more" pagination,但我没有找到正确的答案。

我想废弃所有项目的数据,例如全部 118 个,而不仅仅是前 20 个。

到目前为止我的代码:

    import requests
    import pandas
    from bs4 import BeautifulSoup

    r=requests.get("https://www.landefeld.de/gruppe/en/straight-screwed-connection-metric-/GE4LLM")
    c=r.content
    soup=BeautifulSoup(c,"html.parser")
    all=soup.find_all("div",{"class","article-container"})

    l=[]
    for item in all:
        d={}
        d["Artikel"]=item.find("a",{"class","article-number"}).text.strip()
        d["Bezeichnung"]=item.find("a",{"class","article-name"}).text.strip()
        d["Bild"]="http://landefeld.de" + item.find("img",{"class","article-image"})['src'].strip()
        try:
            d["Material"]=item.find("p",{"class","material"}).text.strip()
        except:
            pass

        props=item.find_all("p",{"class","property"})
        val=item.find_all("p",{"class","value"})
        for p, v in zip(props,val):
            d[p.text]=v.text

        l.append(d)

    df=pandas.DataFrame(l)
    df.to_excel("Output.xlsx")

我在 Sources 中找到了这段 JavaScript 代码:

  // Ersetzt die angezeigte Artikelliste durch eine neue, per Ajax geholte
  Shop.Artikelliste.prototype.reload = function(callback) {
    this.artikelAngezeigt = 0;
    this._doAjaxRequest(callback);
  };

  // Ergaenzt die bestehende Artikelliste mit einer weiteren, per Ajax geholten
  Shop.Artikelliste.prototype.loadMore = function(callback) {
    $('.js-artikelliste-nachladebutton').addClass('is-loading');
    this._doAjaxRequest(callback);
  };

  // Fuehrt den Ajax-Request aus, uebergibt die callback-Funktion
  // an den Handler fuer das Ajax-Ergebnis.
  Shop.Artikelliste.prototype._doAjaxRequest = function(callback) {
    var request = $.extend({ 'param_1': this.artikelAngezeigt }, this.requestParams);

    var _this = this; // Closure
    Shop.Util.ajaxRequest(request, 1, function(error, result) {
      _this._handleResult(error, result, callback);
      $('.js-artikelliste-nachladebutton').removeClass('is-loading');
    });
  }

  // Laedt die Seite ganz neu
  // (Muss auch nach POST Requests funktionieren)
  Shop.Artikelliste.prototype._doPageRequest = function() {

    var params = [];
    for (var key in this.requestParams) {
      params.push(key + '=' + this.requestParams[key]);
    }
    var queryString = params.join(';');

    var href = window.location.href;
    var i = href.indexOf('?');
    if (i == -1) {
      href += '?' + queryString;
    } else {
      href = href.substring(0, i + 1) + queryString;
    }

    window.location.href = href;
  }

  // Wird mit dem Ergebnis vom Ajax-Request aufgerufen. Fuehrt zunaechst
  // den callback aus, danach wird die Artikelliste ueberschrieben oder ergaenzt,
  // danach der Nachladebutton aktualisiert und ggfalls ausgeblendet.
  Shop.Artikelliste.prototype._handleResult = function(error, result, callback) {
      if (error) { return; }

      if (typeof callback === "function") {
         callback(result);
      }

      if (this.artikelAngezeigt == 0) {
        this.$container.html(result.html)
      } else {
        this.$container.append(result.html)
      }

      this.artikelAngezeigt = this.artikelAngezeigt + parseInt(result.count); 

谁有我的解决方案?

感谢您的帮助

【问题讨论】:

  • 这不是问题。
  • 你有什么问题?您可以显示您的代码并指定您需要的内容。
  • @MrSam 抱歉,不,我正在编辑我的问题...

标签: python web-scraping beautifulsoup


【解决方案1】:

我自己找到了解决方案。

这是一个很好的教程...

http://toddhayton.com/2015/03/11/scraping-ajax-pages-with-python/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2013-10-29
    相关资源
    最近更新 更多