python - Selenium 转换格式数“k”(千)python 获取标题
问题描述
我想用 selenium 提取,instagram 上的数字追随者,但“k”格式(千)阻止我得到这个。
所以我在 python 上尝试了这个:
follower_count = int(browser.find_element_by_xpath("//li//a//span[contains(@title]").text)
但它不起作用,我认为要么用整数替换“k”,要么使用find_element_by_xpath()
提取标题上的“int”
解决方案
您可以通过多种方式替换“k”格式(千),如下所示:
使用innerText和
replace()
方法:# follower_count = browser.find_element_by_xpath("//li//a//span[contains(@title)]").text follower_count = "170k" print(int(follower_count.replace("k","000"))) # 170000 print(type(int(follower_count.replace("k","000")))) # <class 'int'>
使用title属性和
re.sub()
方法:# follower_count = browser.find_element_by_xpath("//li//a//span[contains(@title)]").get_attribute("title") import re follower_count = "170,125" print(int(re.sub(',', '', follower_count))) # 170125 print(type(int(re.sub(',', '', follower_count)))) # <class 'int'>
参考
您可以在以下位置找到一些相关的详细讨论:
推荐阅读
- matlab - 在 MATLAB 中使用“conv()”仅计算某个区间的卷积
- r - 在ggplot2中用bquote折叠字幕
- javascript - 如何将 js 或任何函数的结果返回到 iPhone 的屏幕?通过内容视图
- java - 使用 Snowflake 连接器启动 Kafka Connect 时出现 org.apache.kafka.connect.errors.ConnectException 错误
- python - NumPy 从多个文件中获取数据
- javascript - HTML 服务器端包含问题
- javascript - 如何将 settimeout 更改为函数而不是字符串
- c - 为什么 pop 函数不使用 stack[*top--] 运行,而是使用 stack[*(top--)] 运行?
- swift - 无法为表达式生成诊断 - 列出测试数据
- java - 如何使用 toString() 在 Java 中发送整数链表?