BeautifulSoup的string是navigablestring,不能存入数据库咋办
https://www.crummy.com/software/BeautifulSoup/bs4/doc/#navigablestring话说错误日志都告诉你是因为传入的参数是个navigablestring了才出问题的情况下,你是如何得到type是unicode的结论的?
■网友
参照这个 The first option is the correct way to put query parameters into the query - it is called a parameterized query. In this case, you are letting the database driver to escape the query parameters, safely insert them into the query and handle the Python-to-MySQL type conversions.The error you are getting means that it could not convert one of the ID, Record, Latitude, Longitude or code parameter values to a valid MySQL database type. To be specific, see the variable types you have posted:ID \u0026lt;type \u0026#39;unicode\u0026#39;\u0026gt; Record \u0026lt;type \u0026#39;unicode\u0026#39;\u0026gt;Latitude \u0026lt;class \u0026#39;bs4.element.NavigableString\u0026#39;\u0026gt;Longitude \u0026lt;class \u0026#39;bs4.element.NavigableString\u0026#39;\u0026gt;code \u0026lt;type \u0026#39;unicode\u0026#39;\u0026gt;The problem is with Latitude and Longitude - they are BeautifulSoup\u0026#39;s NavigableString class instances - the MySQL converter having difficulties in understanding how to convert a NavigableString object into a valid MySQL type. Convert them to strings explicitly beforehand:update = """ INSERT INTO myDB.newtable (ID,Record,Latitude,Longitude,code) VALUES (%s,%s,%s,%s,%s)"""cursor2.execute(update, (ID, Record, str(Latitude), str(Longitude), code))
推荐阅读
- BeautifulSoup处理html文档的过程是怎么样的分为几个步骤
- 关于get请求url中的queryString与#号的问题
- 写一个类,让toString()输出这个类的源码
- Java使用StringBuilder进行简单字符串替换能提高效率,为啥jdk不新增一个这样的实现
- 关于Python BeautifulSoup解析页面内容丢失的问题
- win7环境下安装BeautifulSoup出现SyntaxError,请问是啥原因
- 怎样在 Sphinx 的 PHP 扩展中读取 string attibutes?
- 查找替换制表符\\t,为啥这三种写法都可以
- 为啥这代码运行结果会出现NavigableString对象
- 写爬虫的时候,Beautifulsoup 遇到标签没有合上的网页应该咋提取?
