【问题标题】:How to scrape card details using beautiful soup and python如何使用漂亮的汤和 python 刮卡细节
【发布时间】:2021-02-16 18:16:10
【问题描述】:

我正在尝试抓取此链接: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%

基本上我想获取该页面中每张卡片的详细信息

我尝试了不同的标签,但似乎都没有效果。

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

非常感谢任何帮助。

【问题讨论】:

  • 由于您还需要 Get Unlimited Cashback 之类的标题,因此您对每张卡详细信息到底想要什么?
  • @NoobCoder 我不想要标题我只想要描述。

标签: python web-scraping beautifulsoup python-requests scrapinghub


【解决方案1】:

会发生什么?

您的主要问题是,该网站动态地提供其内容,而您不会达到您的目标,即您请求它的方式。打印你的汤看看,它不会包含你在浏览器中检查的元素。

如何解决?

使用 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

【讨论】:

  • 感谢您的恰当回复,请您解释一下这个选择器在做什么'#ulCreditCard li li > span'
  • 很高兴为您提供帮助 - 您仍然可以使用您的选择,但它不像 css selector 那样具体,它指向 ID 为 ulul 及其 lis里面有lis
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 2017-12-23
  • 1970-01-01
  • 2018-05-22
  • 1970-01-01
相关资源
最近更新 更多