Coder Social home page Coder Social logo

Comments (6)

howie6879 avatar howie6879 commented on September 26, 2024

你好 @Ehco1996

  • 关于POST请求请见 #106 ,对于发送ajax请求的data,目前解决是请先用data拼接好形成url再请求,或者自己构建route,然后定义视图函数自己构建,很灵活
  • 对于第二点,不是很明白你的意思,想要获取h1下所有的文本(包括子标签)你是指获取html?

from toapi.

Ehco1996 avatar Ehco1996 commented on September 26, 2024

@howie6879

谢谢回复

  • post的data 和url没有关系吧,我的意思是不知道怎么 通过post方式获得items里的数据

  • 第二点: 内置的XPath 方法返回的不是一个标准的 lxml 里的element对象,所以不能直接调用xpath内置的一些方法

from toapi.

howie6879 avatar howie6879 commented on September 26, 2024

你好 @Ehco1996

  • #106 里面的post例子可以解决你的问题么?
  • Xpath默认如果元素只有一个会返回text,如果你需要返回etree,可以使用内置的cssselect方式

from toapi.

Ehco1996 avatar Ehco1996 commented on September 26, 2024

@howie6879

谢谢回复!

第一问题我看到这样一个代码片段:

api = Api(url)
app = api.server.app

@app.route('/post_page/')
def post_method():
    res = requests.post(url, data) # You need to analysis the ajax post request of source site.
    return  item.parse(res.text)

请问这段是需要加在app.py里么?
我大概能看懂这是在flask里添加了一个post的路由

可是最后返回的 item.parse(res.text) 的item是从哪里来的呢?
还有这个路由是怎么和 Toapi Item 类发生联动的呢?

  • 第二个问题解决了

再次感谢 !

from toapi.

howie6879 avatar howie6879 commented on September 26, 2024

你好 @Ehco1996
我将用下面的代码片段来解决你说的问题:

    @api.server.app.route(/post_page/')
    def post_page():
        html = requests.post(url, data) 
        results = {}
        # 定义一个Item列表,意思是你获取的html后,需要按照下面几个你自己定义的Item获取数据
        items = [Item1,Item2]
        for item in items:
            parsed_item = api.parse_item(html, item['item'])
            results[item].__name__] = parsed_item
        return api.server.app.response_class(
            response=json.dumps(results),
            status=200,
            mimetype='application/json'
        )

明白了么?

from toapi.

Ehco1996 avatar Ehco1996 commented on September 26, 2024

@howie6879
明白了!太感谢了!

from toapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.