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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。