如何使用beautiful soup和python刮卡细节

2024-09-27 23:27:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试删除此链接:https://www.axisbank.com/retail/cards/credit-card

使用以下代码

from urllib.request import urlopen
from bs4 import BeautifulSoup
import json, requests, re

axis_url = ["https://www.axisbank.com/retail/cards/credit-card"]

html = requests.get(axis_url[0])
soup = BeautifulSoup(html.content, 'lxml')

for d in soup.find_all('span'):
    print(d.get_text())

输出:

close
5.15%
%
4.00%
%
5.40%

基本上,我想得到每一张卡片的详细信息

enter image description here

我尝试了不同的标签,但似乎没有一个有效

我很高兴看到代码满足我的要求

非常感谢您的帮助


Tags: 代码fromhttpsimportcomurlwwwcard
1条回答
网友
1楼 · 发布于 2024-09-27 23:27:28

会发生什么

你的主要问题是,网站动态地提供它的内容,而你不会得到你的目标,你所要求的wa。打印您的汤并查看,它将不包含您在浏览器中检查的元素

如何修复

使用selenium,它可以处理动态生成的内容,并提供您已经检查过的信息:

示例

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome(executable_path=r'C:\Program Files\ChromeDriver\chromedriver.exe')
url = 'https://www.axisbank.com/retail/cards/credit-card'
driver.get(url)

soup = BeautifulSoup(driver.page_source, 'lxml')
    
driver.close()

textList = []
for d in soup.select('#ulCreditCard li li > span'):
        textList.append(d.get_text('^^', strip=True))
    
textList

相关问题 更多 >

    热门问题