duanreng3439 2017-05-19 15:50
浏览 249
已采纳

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

I'm interested in scrapping data from a website, and pass the data for use it in 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.

The issue I have is that I'm unsure how to serialise the Python data.

I'm using Selenium, and I have the following code.

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.

  • 写回答

1条回答 默认 最新

  • doue1925 2017-05-19 16:25
    关注

    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:

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用