简体   繁体   English

Python初学者:将Selenium WebElement对象传递给PHP

[英]Python Beginner: Passing Selenium WebElement Object to PHP

I'm interested in scrapping data from a website, and pass the data for use it in PHP. 我有兴趣从网站上删除数据,并传递数据以便在PHP中使用它。 I have had a look around, and the best suggestion I have been able to find is to first serialise the Python data and then pass it along. 我已经浏览了一下,我能找到的最好的建议是首先序列化Python数据,然后传递它。

The issue I have is that I'm unsure how to serialise the Python data. 我遇到的问题是我不确定如何序列化Python数据。

I'm using Selenium, and I have the following code. 我正在使用Selenium,我有以下代码。

test = browser.find_elements_by_css_selector("table#resultstable td")

I can see the data I want to use by running the variable through a loop and printing it. 我可以通过循环运行变量并打印它来查看我想要使用的数据。

for val in test:
    print(val.text)

However when I try to serialise the object, I receive the following error: 但是,当我尝试序列化对象时,我收到以下错误:

json.dumps(test)
....
TypeError: Object of type 'WebElement' is not JSON serializable!

I Hope somebody can point me in the right direction, I'm happy in PHP but I have only begun to look Python recently. 我希望有人可以指出我正确的方向,我很高兴PHP,但我最近才开始看Python。

JSON the data format is limited in what it can store (numbers, strings, booleans, and then arrays or maps (what python calls dictionaries) of these elements (or other arrays or maps which at some point end in one of those formats). json the python library for manipulating json data can therefore only dump to a json string something that fits those rules. In your case, I'd suggest as a simple starting point: JSON数据格式受限于它可以存储的内容(数字,字符串,布尔值,然后是数组或映射(python调用字典)这些元素(或其他数组或映射,在某些点以其中一种格式结束)。 json用于操作json数据的python库因此只能转换为符合这些规则的json字符串。在你的情况下,我建议作为一个简单的起点:

columns = [val.text for val in test] # convert to a list of strings, where each string is the td.text

json.dumps(columns) should then give you something useful json.dumps(columns)应该给你一些有用的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM