猿问

如何单击/使用从Python中的Beautiful Soup解析的链接

你好,我是python的初学者,我对从这里去哪里感到困惑


使用bs4搜索后,如何与特定链接进行交互/单击?因此,在我的脚本中,我使用bs4搜索链接,这些链接可以在网页中单击,并带有特定的关键字,因此我单击了正确的产品。


onSale = False  # Set while loop variable

shoeName = 'moon'  # Set keyword to look for

browser = webdriver.Chrome()



browser.get(r'https://www.nike.com/launch/?s=upcoming')  # Get URL to scan

soup = BeautifulSoup(browser.page_source, 'html.parser')  # Convert URL to a soup object



while onSale is False:  # Create loop to keep checking inventory


for link in soup.find_all('a', class_=r'card-link d-sm-b'):

    shoeCode = str((link.get('href', None), link.get_text()))

    compareName = re.sub("[^\w]", " ", shoeCode.lower()).split()  # Takes the link and converts it into a string


    if shoeName in compareName:  # Checks to see if the keyword is used


       # Interact/Click link


    else:

        print(shoeCode)


        continue

找到正确的链接后,如何使用它与网站进行交互?我是否使用selenium,urllib和or请求?谢谢!


长风秋雁
浏览 161回答 1
1回答

海绵宝宝撒

您可以使用硒单击链接,您可以在此处查看如何执行此操作。或者,在获取包含请求的页面(忘记urllib)并使用bs4提取URL之后,您可以进行操作requests.get('your_example_url')并再次获取结果。
随时随地看视频慕课网APP

相关分类

Python
我要回答