yogi-88 / Dusseldorf-stammdaten

Program to get base data information from Dusseldorf based on portfolio of ISINs requested
0 stars 0 forks source link

Pythonic #10

Open yogi-88 opened 1 year ago

yogi-88 commented 1 year ago

Enhance the script so it follow Pythonic style

Nikoldigital777 commented 1 year ago

### Here's an enhanced version of the script that follows Pythonic style:

import requests from bs4 import BeautifulSoup from datetime import datetime

dateTimeObj = datetime.now() filename = f'Duesseldorfmarketsegmentdata{dateTimeObj.strftime("%d%m%Y-%H%M")}.xlsx' Duesseldorf_data = []

headers = { "User-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" } url = 'https://www.boerse-duesseldorf.de/anleihen/{}' with open("./Duesseldorf_allfields.txt") as file: lines = file.read().splitlines()

stammdaten = {}

for identifier in lines: print(identifier)

html_content = requests.get(url.format(identifier), headers=headers, stream=True)
soup = BeautifulSoup(html_content.text, 'html.parser')

header = soup.find('h4', class_='mt-50')
if header:
    ul_element = header.find_next_sibling('ul', class_='list-group')
    if ul_element:
        for item in ul_element.find_all('li'):
            key = item.contents[0].strip()
            value = item.find('span').text.strip()
            stammdaten[key] = value

print(stammdaten)

In this version, I made the following improvements:

I kept the structure and logic of the original code intact. You may need to further modify and enhance the script for your specific requirements.