与美丽的小组及其隔离刮擦&硒

发布于 2025-02-13 17:11:52 字数 1169 浏览 2 评论 0原文

我正在尝试从HREF获取“ Master_key”。有人可以帮助我隔离文字中的钥匙吗?理想情况下,我将能够运行一个find_all并获得所有可用的find_all。谢谢!!

from bs4 import BeautifulSoup as bs
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
import requests

options = Options()
options.headless = False
driver = webdriver.Firefox(options=options)

driver.get("https://annual.asaecenter.org/expo.cfm?")

driver.find_element(By. XPATH, "//*[@id='clickAgreeCookie']").click()

driver.find_element(By. XPATH, "//*[@id='search_table']/tbody/tr[7]/td[2]/input[2]").click()

page = bs(driver.page_source, 'html.parser')

key = page.find("a", href="javascript:void(0)")

print(key)

输出:

<a href="javascript:void(0)" onclick="javascript:ExhibitorPopup('profile.cfm?profile_name=exhibitor&amp;master_key=F4DAF300-9A26-4024-9E49-3E29116E5A36&amp;inv_mast_key=93A17E5D-A46F-F21E-77E4-77B38A3B30EE&amp;xtemplate','exhibitor_profile');;analytics(1,'','F4DAF300-9A26-4024-9E49-3E29116E5A36')">108 Ideaspace</a>

所需输出:

F4DAF300-9A26-4024-9E49-3E29116E5A36

I am trying to grab a "master_key" from an href. Could someone please help me isolate the key out of the text? Ideally, I would be able to run a find_all and get all the ones available. Thanks!!

from bs4 import BeautifulSoup as bs
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
import requests

options = Options()
options.headless = False
driver = webdriver.Firefox(options=options)

driver.get("https://annual.asaecenter.org/expo.cfm?")

driver.find_element(By. XPATH, "//*[@id='clickAgreeCookie']").click()

driver.find_element(By. XPATH, "//*[@id='search_table']/tbody/tr[7]/td[2]/input[2]").click()

page = bs(driver.page_source, 'html.parser')

key = page.find("a", href="javascript:void(0)")

print(key)

Output:

<a href="javascript:void(0)" onclick="javascript:ExhibitorPopup('profile.cfm?profile_name=exhibitor&master_key=F4DAF300-9A26-4024-9E49-3E29116E5A36&inv_mast_key=93A17E5D-A46F-F21E-77E4-77B38A3B30EE&xtemplate','exhibitor_profile');;analytics(1,'','F4DAF300-9A26-4024-9E49-3E29116E5A36')">108 Ideaspace</a>

Desired output:

F4DAF300-9A26-4024-9E49-3E29116E5A36

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2025-02-20 17:11:52

您将必须使用正则。使用内置 re 模块。例如:

import re

key = """<a href="javascript:void(0)" onclick="javascript:ExhibitorPopup('profile.cfm?profile_name=exhibitor&master_key=F4DAF300-9A26-4024-9E49-3E29116E5A36&inv_mast_key=93A17E5D-A46F-F21E-77E4-77B38A3B30EE&xtemplate','exhibitor_profile');;analytics(1,'','F4DAF300-9A26-4024-9E49-3E29116E5A36')">108 Ideaspace</a>"""

print(re.search("master_key=(.*?)&", key).group(1))

这将搜索“ master_key =”和“&amp; amp”之间的所有文本。
在您的情况下,您需要使用:

key = page.find("a", href="javascript:void(0)")
print(re.search("master_key=(.*?)&", str(key)).group(1))

输出:

F4DAF300-9A26-4024-9E49-3E29116E5A36

You'll have to use regex. using the built-in re module. For example:

import re

key = """<a href="javascript:void(0)" onclick="javascript:ExhibitorPopup('profile.cfm?profile_name=exhibitor&master_key=F4DAF300-9A26-4024-9E49-3E29116E5A36&inv_mast_key=93A17E5D-A46F-F21E-77E4-77B38A3B30EE&xtemplate','exhibitor_profile');;analytics(1,'','F4DAF300-9A26-4024-9E49-3E29116E5A36')">108 Ideaspace</a>"""

print(re.search("master_key=(.*?)&", key).group(1))

This searches for all the text between "master_key=" and "&".
In your case, you'll need to use:

key = page.find("a", href="javascript:void(0)")
print(re.search("master_key=(.*?)&", str(key)).group(1))

Output:

F4DAF300-9A26-4024-9E49-3E29116E5A36
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文