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 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败