首页 > 编程问答 >试图找出此页面的逻辑:存储了大约 ++ 100 个结果 - 并使用 Python 和 BS4 进行了解析

试图找出此页面的逻辑:存储了大约 ++ 100 个结果 - 并使用 Python 和 BS4 进行了解析

时间:2024-07-26 05:28:09浏览次数:10  
标签:python pandas web-scraping beautifulsoup

试图找出此页面背后的逻辑:

我们已将一些结果存储在以下数据库中:

https://www.raiffeisen.ch/rch/de/ueber-uns/raiffeisen-gruppe/ Organization/raiffeisenbanken/deutsche-schweiz.html#accordionitem_18104049731620873397

a到z大约:120个结果或更多:

我们必须使用哪些选项来获取数据

https://www.raiffeisen .ch/zuerich/de.html#bankselector-focus-titlebar

Raiffeisenbank Zürich
Limmatquai 68
8001Zürich
Tel. +41 43 244 78 78
[email protected]

https://www.raiffeisen.ch/sennwald/de.html

Raiffeisenbank Sennwald
Äugstisriet 7
9466Sennwald
Tel. +41 81 750 40 40
[email protected]
BIC/Swift Code: RAIFCH22XXX

https://www.raiffeisen.ch/basel/de/ueber -uns/engagement.html#bankselector-focus-titlebar

Raiffeisenbank Basel
St. Jakobs-Strasse 7
4052Basel
Tel. +41 61 226 27 28
[email protected]

嗯 - 我认为 - 如果以某种方式将所有内容都封装在 url 编码块中...

好吧,我正在尝试找出它 - 这是我的方法:

import requests
from bs4 import BeautifulSoup

def get_raiffeisen_data(url):
    response = requests.get(url)
    if response.status_code == 200:
        soup = BeautifulSoup(response.content, 'html.parser')
        banks = []

        # Find all bank entries
        bank_entries = soup.find_all('div', class_='bank-entry')

        for entry in bank_entries:
            bank = {}
            bank['name'] = entry.find('h2', class_='bank-name').text.strip()
            bank['address'] = entry.find('div', class_='bank-address').text.strip()
            bank['tel'] = entry.find('div', class_='bank-tel').text.strip()
            bank['email'] = entry.find('a', class_='bank-email').text.strip()
            banks.append(bank)
        
        return banks
    else:
        print(f"Failed to retrieve data from {url}")
        return None

url = 'https://www.raiffeisen.ch/rch/de/ueber-uns/raiffeisen-gruppe/organisation/raiffeisenbanken/deutsche-schweiz.html'
banks_data = get_raiffeisen_data(url)

for bank in banks_data:
    print(f"Name: {bank['name']}")
    print(f"Address: {bank['address']}")
    print(f"Tel: {bank['tel']}")
    print(f"Email: {bank['email']}")
    print('-' * 40)

提供的代码示例无法使用,因为它假设数据直接嵌入在提供的初始 URL 的 HTML 中。但是,根据的描述,数据似乎是从其他地方动态加载的。

为了从 Raiffeisen 网站抓取所有 ~120 家银行的数据,需要确定加载这些数据的机制。这可能涉及以下一项或多项操作:

  1. 检查网站的 JavaScript 代码: 网站可能正在使用 JavaScript 向服务器发出 AJAX 请求以检索银行数据。可以使用浏览器的开发者工具(网络选项卡)来分析网站发出的网络请求,并识别检索银行数据的请求。

  2. 查找隐藏的 API 端点: 有些网站使用 API 来提供数据。通过分析网络请求,可以找到提供银行数据的隐藏 API 端点。

  3. 逆向工程网站的 JavaScript 代码: 如果数据是使用复杂逻辑或混淆的 JavaScript 代码加载的,可能需要花费更多精力来理解代码的工作原理,并复制加载数据的逻辑。

找到加载数据的机制后,可以使用 Python 和 requests 库来模拟这些请求,并使用 Beautiful Soup 或其他解析库(例如 lxml 或 json)来提取数据。

以下是一些可能帮助进行调查的附加提示:

  • 搜索特定关键字: 在网络选项卡中,尝试搜索“bank”、“location”或“branch”等关键字,以过滤与加载银行数据相关的请求。
  • 检查请求头和参数: 分析网络请求中的请求头和参数,以查看是否有任何模式或唯一标识符用于检索特定数据。
  • 使用调试器: 使用浏览器的 JavaScript 调试器来设置断点并逐步执行代码,以了解数据加载过程。

请记住,抓取网站时请务必遵守其服务条款和 robots.txt 文件。

标签:python,pandas,web-scraping,beautifulsoup
From: 78788919

相关文章

  • 如何在 Numpy Python 中将 4 维数组的下三角形复制到上三角形?
    目标是将下三角形复制到上三角形。根据OP中提出的建议,起草了以下代码。importnumpyasnplw_up_pair=np.tril_indices(4,-1)arr=np.zeros((4,4,1,1))arr[1,:1,:,0]=1arr[2,:2,0,0]=2arr[3,:3,0,0]=3arr=arr+arr.T-np.diag(np.diag(arr))但是,它......
  • 如何在 Python 中对多行使用单个 INSERT INTO 语句?
    我目前正在开发一个DiscordPython机器人,我在其中循环遍历ForumTags列表,并为每个对象生成INSERTINTOSQL语句以将数据插入MySQL数据库。但是,我想要通过将所有这些单独的INSERTINTO语句组合到单个查询中来优化我的代码,如下所示:INSERTINTO......
  • 双 for 循环的 Pythonic 方式
    我有以下代码:importnumpyasnpepsilon=np.array([[0.,0.00172667,0.00071437,0.00091779,0.00154501],[0.00128983,0.,0.00028139,0.00215905,0.00094862],[0.00035811,0.00018714,0.,0.00029365,0.00036993......
  • 在 matplotlib 中绘制一个字符串函数 // 将 str 解释为 python 代码?
    我正在创建一个RPN计算器,尝试绘制用户给出的函数。例如,如果用户输入"xsin3*plot"我希望它绘制sin(x)*3其代码如下。注意:问题在ifprompt=="plot"userInputX=""#userInputXisalwaysreplacedbefore......
  • Python (Pebble) - 超时功能。当 TimeoutError 发生时,获取从 iterable 传递给函数的值
    我正在尝试在Pebble中设置工作超时(基本上有效)frompebbleimportProcessPoolfrommultiprocessingimportProcess,Pool,cpu_countimporttimedeftest_fn(randomNumberFromList):#print(f'Beginngingforthisnumber:{randomNumberFromList}')ifr......
  • 为什么在 Python 上使用正则表达式组功能会给出不同的输出
    importrestring1="aaabaa"zusuchen="aa"#1m_start=re.finditer(fr'(?=({zusuchen}))',string1)results=[(match.start(1),match.end(1)-1)formatchinm_start]forzinresults:print(z)print("Now#2:"......
  • 如何在python3中找到文件的长度?
    我的第一个.py:defcreate_file(file_name):list=["ab","cd","ef"]foriinlist:withopen(file_name,"a+")asinput_file:print("{}".format(i),file=input_file)我的第二个.py:fromfirstimport......
  • 哪种 python 日志记录风格是推荐的或标准的?
    我是Python新手。介于以下2个选项之间。对于python来说,推荐哪种风格或者更好?logging.info(f"Won'tsavemodelasscoreisbelow0,score:{score}")logging.info("Won'tsavemodelasscoreisbelow0,score%s",score)我个人更喜欢第二种方法。在Python......
  • python 协程 自定义互斥锁
    最近在用python的一款异步web框架sanic搭建web服务,遇到一个需要加特定锁的场景:同一用户并发处理订单时需要排队处理,但不同用户不需要排队。如果仅仅使用asyncwithasyncio.Lock()的话。会使所有请求都排队处理。1importasyncio2importdatetime34lock=asyncio.L......
  • Python 获取tiktok视频评论回复数据 api接口
    TIKTOKapi接口爬取tiktok视频评论回复数据详细采集页面如图https://www.tiktok.com/@dailymail/video/7329872821990182190?q=neural%20link&t=1706783508149请求APIhttp://api.xxxx.com/tt/video/info/comment/reply?video_id=7288909913185701125&comment_id=7294900......