如何使用 bs4 和 selenium 仅抓取元素中文本的特定部分?
我正在尝试使用 BeatifulSoup4 抓取发送到电子邮件的验证码,并使用 selenium 将其粘贴到验证字段中。这是我用来提取元素内部文本的代码:
soup = BeautifulSoup(driver.page_source)
number_code = soup.find(class_="sms-text").text
verification_field = driver.find_element_by_name("q")
verification_field.send_keys(number_code)
但是,这将抓取元素内部的所有文本,而不仅仅是我需要的验证代码。谁能告诉我如何剪掉不需要的文本部分,以便只得到数字代码?
PS:这是我在这里发表的第一篇文章,我是一个完全的新手,所以请放轻松:)
I am trying to scrape a verification code sent to an email with BeatifulSoup4 and paste it into a verification field using selenium. This is the code that I use to extract the text inside of the element:
soup = BeautifulSoup(driver.page_source)
number_code = soup.find(class_="sms-text").text
verification_field = driver.find_element_by_name("q")
verification_field.send_keys(number_code)
However this will scrape all the text inside the element and not only the verification code I need. Can anyone tell how I can cut out the part of the text which I dont need so that I only get the number code?
PS: this was my very first post here and Im a complete newbie so please take it easy on me :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的理解是,它
给了你整个文本,你只需要一个特定的部分。那么,您可以使用字符串方法来清理文本。
例如,如果文本是“您的验证码是#####”并且您想要的部分始终位于句子的末尾,您可以使用
这将带您返回需要使用 selenium 输入的代码
What I understand is that
gives you the entire text and you just need an specific part. Well, you can use strings methods in order to clean the text.
For example if the text is "Your verification code is #####" and the part you want is always at the end of the sentence, you can use
This will bring you back just the code that you need to type using selenium