如何在没有明显可识别区别的情况下导航表格?

huangapple go评论55阅读模式
英文:

How do I navigate a table without any easily accessible distinctions?

问题

我现在是你的中文翻译,这部分代码不需要翻译。你遇到的问题是如何提取星期几或日期以及相应的时间,但不知道如何操作。你注意到教授和他们的时间段之间只有一个较短的tr,里面只有一个td,而不是通常的两个,但不知道如何使用它。是否有一种聪明的方法来实现这个目标?

英文:

I'm working on a small project to learn python better (right now BeautifulSoup), and got stuck with no idea how to move forward. I am trying to scrape data from www.we.umg.edu.pl/ktm/konsultacje.

As for now I have this part of code:

import requests
from bs4 import BeautifulSoup

# Make a GET request to the URL
url = "https://we.umg.edu.pl/ktm/konsultacje"
response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')

roomsProfessors = soup.find_all('h3')

# Lists for sorted data
professors = []
rooms = []

# Get data about rooms and professors and fill their lists
for roomProfessor in roomsProfessors:
    professor = []

    for child in roomProfessor:
        if child.name == 'br':
            continue

        child = child.string.replace('\t', '').replace('\n', '')

        if 'pok.' in child:
            rooms.append(child)
        else:
            professor.append(child)
        
    professors.append(professor)

However I got stuck right now. I want to extract weekdays or dates with the corresponding hour, but have no idea how to do it.

I noticed that the part with professor and their hours is separated with one shorter tr with just one td inside instead of usual two, but have no idea how to use it.

Is there some smart way to achieve this?

答案1

得分: 1

这是你要翻译的代码部分:

Not sure what your expected output should look like, but you could iterate with `find_next_siblings()` or `previous_siblings()`

import requests
from bs4 import BeautifulSoup
url = "https://we.umg.edu.pl/ktm/konsultacje"
response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')
data = []
for p in soup.select('tr:has(h3)'):
    d ={
        'prof': p.h3.contents[0].strip(),
        'room': p.h3.contents[-1].strip(),
        'times': []
    }

    for e in p.find_next_siblings('tr'):
        if e.h3:
            break
        if len(e.text.strip()) > 1 and not e.h5:
            d['times'].append(e.get_text('|',strip=True))
    data.append(d)
data

这是翻译后的内容,只包括代码部分:

不确定您期望的输出应该是什么但您可以使用`find_next_siblings()``previous_siblings()`进行迭代

import requests
from bs4 import BeautifulSoup
url = "https://we.umg.edu.pl/ktm/konsultacje"
response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')
data = []
for p in soup.select('tr:has(h3)'):
    d ={
        'prof': p.h3.contents[0].strip(),
        'room': p.h3.contents[-1].strip(),
        'times': []
    }

    for e in p.find_next_siblings('tr'):
        if e.h3:
            break
        if len(e.text.strip()) > 1 and not e.h5:
            d['times'].append(e.get_text('|',strip=True))
    data.append(d)
data
英文:

Not sure what your expected output should look like, but you could iterate with find_next_siblings() or previous_siblings()

Example
import requests
from bs4 import BeautifulSoup
url = "https://we.umg.edu.pl/ktm/konsultacje"
response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')
data = []
for p in soup.select('tr:has(h3)'):
    d ={
        'prof': p.h3.contents[0].strip(),
        'room': p.h3.contents[-1].strip(),
        'times': []
    }

    for e in p.find_next_siblings('tr'):
        if e.h3:
            break
        if len(e.text.strip()) > 1 and not e.h5:
            d['times'].append(e.get_text('|',strip=True))
    data.append(d)
data
Output
[{'prof': 'dr hab. inż. Andrzej Borys, prof. UMG',
  'room': 'pok. C28',
  'times': ['czwartek|12.00-14.00',
   '11.03|09.00-10.00',
   '25.03|09.00-10.00',
   '15.04|09.00-10.00',
   '29.04|09.00-10.00',
   '10.06|10.00-11.00']},
 {'prof': 'mgr inż. Dawid Budnarowski',
  'room': 'pok. C-51',
  'times': ['wtorek|środa|11.00-12.00|11.00-12.00']},
 {'prof': 'dr inż. Tomasz Ciszewski',
  'room': 'pok. C345',
  'times': ['środa|15.00-17.00']},
 {'prof': 'dr inż. Wiesław Citko',
  'room': 'pok. C341',
  'times': ['czwartek|9.00-11.00',
   '04.03|11.00-12.00',
   '01.04|10.00-11.00',
   '20.05|12.00-13.00',
   '16.06|19.00-20.00',
   '07.07|16.00-17.00']},...]

答案2

得分: 1

以下是使用pandas的代码:

import pandas as pd
import requests
from tabulate import tabulate

konsultacje = "https://we.umg.edu.pl/ktm/konsultacje"
df = (
    pd
    .read_html(requests.get(konsultacje).text, flavor="lxml")[0]
    .dropna(axis=0, how="all")
)
df.columns = ["Kolumna 1", "Kolumna 2"]
df = df[["Kolumna 1", "Kolumna 2"]]
print(tabulate(df, headers="keys", tablefmt="psql", showindex=False))

输出:

+--------------------------------------------------------+--------------------------------------------------------+
| Kolumna 1                                              | Kolumna 2                                              |
|--------------------------------------------------------+--------------------------------------------------------|
| dr hab. inż. Andrzej Borys, prof. UMG  pok. C28        | dr hab. inż. Andrzej Borys, prof. UMG  pok. C28        |
| studia stacjonarne                                     | studia stacjonarne                                     |
| czwartek                                               | 12.00-14.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 11.03                                                  | 09.00-10.00                                            |
| 25.03                                                  | 09.00-10.00                                            |
| 15.04                                                  | 09.00-10.00                                            |
| 29.04                                                  | 09.00-10.00                                            |
| 10.06                                                  | 10.00-11.00                                            |
| mgr inż. Dawid Budnarowski  pok. C-51                  | mgr inż. Dawid Budnarowski  pok. C-51                  |
| studia stacjonarne                                     | studia stacjonarne                                     |
| wtorek  środa                                          | 11.00-12.00  11.00-12.00                               |
| dr inż. Tomasz Ciszewski  pok. C345                    | dr inż. Tomasz Ciszewski  pok. C345                    |
| studia stacjonarne                                     | studia stacjonarne                                     |
| środa                                                  | 15.00-17.00                                            |
| dr inż. Wiesław Citko  pok. C341                       | dr inż. Wiesław Citko  pok. C341                       |
| studia stacjonarne                                     | studia stacjonarne                                     |
| czwartek                                               | 9.00-11.00                                             |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 04.03                                                  | 11.00-12.00                                            |
| 01.04                                                  | 10.00-11.00                                            |
| 20.05                                                  | 12.00-13.00                                            |
| 16.06                                                  | 19.00-20.00                                            |
| 07.07                                                  | 16.00-17.00                                            |
| dr inż. Piotr Kaczorek  pok. C341                      | dr inż. Piotr Kaczorek  pok. C341                      |
| studia stacjonarne                                     | studia stacjonarne                                     |
| czwartek                                               | 11.00-13.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 18.03                                                  | 11.00-12.00                                            |
| 23.04                                                  | 19.00-20.00                                            |
| 13.05                                                  | 13.00-14.00                                            |
| 10.06                                                  | 12.00-13.00                                            |
| dr inż. Karol Korcz, prof. UMG  pok. C349              | dr inż. Karol Korcz, prof. UMG  pok. C349              |
| studia stacjonarne                                     | studia stacjonarne                                     |
| czwartek                                               | 11.00-13.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 05.03                                                  | 13.00-14.00                                            |
| 19.03                                                  | 13.00-14.00                                            |
| 23.04                                                  | 14.00-15.00                                            |
| 07.05                                                  | 12.00-13.00                                            |
| 03.06                                                  | 14.00-16.00                                            |
| dr inż. Ewa Krac  pok. C51                             | dr inż. Ewa Krac  pok. C51                             |
| studia stacjonarne                                     | studia stacjonarne                                     |
| środa  piątek                                          | 10.30-11.30  10.30-11.30                               |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 19.03                                                  | 13.00-14.00                                            |
| 23.04                                                  | 13.00-14.00                                            |
| 04.06                                                  | 14.00.15.00                                            |
| dr inż. Stanisław Lindner  pok. C344                   | dr inż. Stanisław Lindner  pok. C344                   |
| studia stacjonarne                                     | studia stacjonarne                                     |
| czwartek                                               | 13.00-15.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 19.03                                                  | 12.00-13.00                                            |
| 22.04                                                  | 10.00-11.00                                            |
| 06.05                                                  | 11.00-12.00                                            |
| 04.06                                                  | 11.00-12.00                                            |
| 18.06                                                  | 11.00-12.00                                            |
| dr inż. Andrzej Łuksza  pok. C341                      | dr inż. Andrzej Łuksza  pok. C341                      |
| studia stacjonarne                                     | studia stacjonarne                                     |
| środa                                                  | 11.00-13.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 19.03                                                  | 13.00-14.00                                            |
| 23.04                                                  | 13.00-14.00                                            |
| 06.05                                                  | 14.00-15.00                                            |
| 17.06                                                  | 11.00-12.00                                            |
| dr inż. Beata Pałczyńska

<details>
<summary>英文:</summary>

You&#39;re not saying what your expected output should be, but here&#39;s a brute force approach with `pandas`.

```python
import pandas as pd
import requests
from tabulate import tabulate

konsultacje = &quot;https://we.umg.edu.pl/ktm/konsultacje&quot;
df = (
    pd
    .read_html(requests.get(konsultacje).text, flavor=&quot;lxml&quot;)[0]
    .dropna(axis=0, how=&quot;all&quot;)
)
df.columns = [&quot;Kolumna 1&quot;, &quot;Kolumna 2&quot;]
df = df[[&quot;Kolumna 1&quot;, &quot;Kolumna 2&quot;]]
print(tabulate(df, headers=&quot;keys&quot;, tablefmt=&quot;psql&quot;, showindex=False))

Output:

+--------------------------------------------------------+--------------------------------------------------------+
| Kolumna 1                                              | Kolumna 2                                              |
|--------------------------------------------------------+--------------------------------------------------------|
| dr hab. inż. Andrzej Borys, prof. UMG  pok. C28        | dr hab. inż. Andrzej Borys, prof. UMG  pok. C28        |
| studia stacjonarne                                     | studia stacjonarne                                     |
| czwartek                                               | 12.00-14.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 11.03                                                  | 09.00-10.00                                            |
| 25.03                                                  | 09.00-10.00                                            |
| 15.04                                                  | 09.00-10.00                                            |
| 29.04                                                  | 09.00-10.00                                            |
| 10.06                                                  | 10.00-11.00                                            |
| mgr inż. Dawid Budnarowski  pok. C-51                  | mgr inż. Dawid Budnarowski  pok. C-51                  |
| studia stacjonarne                                     | studia stacjonarne                                     |
| wtorek  środa                                          | 11.00-12.00  11.00-12.00                               |
| dr inż. Tomasz Ciszewski  pok. C345                    | dr inż. Tomasz Ciszewski  pok. C345                    |
| studia stacjonarne                                     | studia stacjonarne                                     |
| środa                                                  | 15.00-17.00                                            |
| dr inż. Wiesław Citko  pok. C341                       | dr inż. Wiesław Citko  pok. C341                       |
| studia stacjonarne                                     | studia stacjonarne                                     |
| czwartek                                               | 9.00-11.00                                             |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 04.03                                                  | 11.00-12.00                                            |
| 01.04                                                  | 10.00-11.00                                            |
| 20.05                                                  | 12.00-13.00                                            |
| 16.06                                                  | 19.00-20.00                                            |
| 07.07                                                  | 16.00-17.00                                            |
| dr inż. Piotr Kaczorek  pok. C341                      | dr inż. Piotr Kaczorek  pok. C341                      |
| studia stacjonarne                                     | studia stacjonarne                                     |
| czwartek                                               | 11.00-13.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 18.03                                                  | 11.00-12.00                                            |
| 23.04                                                  | 19.00-20.00                                            |
| 13.05                                                  | 13.00-14.00                                            |
| 10.06                                                  | 12.00-13.00                                            |
| dr inż. Karol Korcz, prof. UMG  pok. C349              | dr inż. Karol Korcz, prof. UMG  pok. C349              |
| studia stacjonarne                                     | studia stacjonarne                                     |
| czwartek                                               | 11.00-13.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 05.03                                                  | 13.00-14.00                                            |
| 19.03                                                  | 13.00-14.00                                            |
| 23.04                                                  | 14.00-15.00                                            |
| 07.05                                                  | 12.00-13.00                                            |
| 03.06                                                  | 14.00-16.00                                            |
| dr inż. Ewa Krac  pok. C51                             | dr inż. Ewa Krac  pok. C51                             |
| studia stacjonarne                                     | studia stacjonarne                                     |
| środa  piątek                                          | 10.30-11.30  10.30-11.30                               |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 19.03                                                  | 13.00-14.00                                            |
| 23.04                                                  | 13.00-14.00                                            |
| 04.06                                                  | 14.00.15.00                                            |
| dr inż. Stanisław Lindner  pok. C344                   | dr inż. Stanisław Lindner  pok. C344                   |
| studia stacjonarne                                     | studia stacjonarne                                     |
| czwartek                                               | 13.00-15.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 19.03                                                  | 12.00-13.00                                            |
| 22.04                                                  | 10.00-11.00                                            |
| 06.05                                                  | 11.00-12.00                                            |
| 04.06                                                  | 11.00-12.00                                            |
| 18.06                                                  | 11.00-12.00                                            |
| dr inż. Andrzej Łuksza  pok. C341                      | dr inż. Andrzej Łuksza  pok. C341                      |
| studia stacjonarne                                     | studia stacjonarne                                     |
| środa                                                  | 11.00-13.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 19.03                                                  | 13.00-14.00                                            |
| 23.04                                                  | 13.00-14.00                                            |
| 06.05                                                  | 14.00-15.00                                            |
| 17.06                                                  | 11.00-12.00                                            |
| dr inż. Beata Pałczyńska  pok. C339                    | dr inż. Beata Pałczyńska  pok. C339                    |
| studia stacjonarne                                     | studia stacjonarne                                     |
| środa                                                  | 13.00-14.00                                            |
| dr inż. Dorota Rabczuk  pok. C339                      | dr inż. Dorota Rabczuk  pok. C339                      |
| studia stacjonarne                                     | studia stacjonarne                                     |
| wtorek                                                 | 14.00-16.00                                            |
| mgr inż. Wojciech Rabczuk  pok. C51                    | mgr inż. Wojciech Rabczuk  pok. C51                    |
| studia stacjonarne                                     | studia stacjonarne                                     |
| poniedziałek                                           | 7.00-8.00, 12.00-13.00                                 |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 05.03                                                  | 07.00-08.00                                            |
| 19.03                                                  | 11.00-12.00                                            |
| dr hab. inż. Agnieszka Rybarczyk, prof. UMG  pok. C345 | dr hab. inż. Agnieszka Rybarczyk, prof. UMG  pok. C345 |
| studia stacjonarne                                     | studia stacjonarne                                     |
| piątek                                                 | 10.00-12.00                                            |
| dr hab. inż. Wiesław Sieńko, prof. UMG  pok. C208      | dr hab. inż. Wiesław Sieńko, prof. UMG  pok. C208      |
| studia stacjonarne                                     | studia stacjonarne                                     |
| czwartek                                               | 11.00-13.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 04.03                                                  | 14:00 - 15:00                                          |
| 18.03                                                  | 14:00 - 15:00                                          |
| 01.04                                                  | 12:00 - 13:00                                          |
| 06.05                                                  | 14:00 - 15:00                                          |
| 20.05                                                  | 15:00 - 16:00                                          |
| 03.06                                                  | 11:00 - 12:00                                          |
| mgr inż. Marta Szarmach  pok. C339                     | mgr inż. Marta Szarmach  pok. C339                     |
| studia stacjonarne                                     | studia stacjonarne                                     |
| wtorek                                                 | 10.00-12.00                                            |
| Agata Śledzińska-Płotczyk  pok. C-326                  | Agata Śledzińska-Płotczyk  pok. C-326                  |
| studia stacjonarne                                     | studia stacjonarne                                     |
| piątek                                                 | 15.30-16.30                                            |
| mgr inż. Marcin Waraksa  pok. C336                     | mgr inż. Marcin Waraksa  pok. C336                     |
| studia stacjonarne                                     | studia stacjonarne                                     |
| wtorek                                                 | 15.00-17.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 05.03                                                  | 14.00-15.00                                            |
| 19.03                                                  | 14.00-15.00                                            |
| 02.04                                                  | 16.00-17.00                                            |
| 14.04                                                  | 16.00-17.00                                            |
| 30.04                                                  | 13.00-14.00                                            |
| dr hab. inż. Andrzej Żak, prof. UMG  pok. C326         | dr hab. inż. Andrzej Żak, prof. UMG  pok. C326         |
| studia stacjonarne                                     | studia stacjonarne                                     |
| wtorek                                                 | 16.00-18.00                                            |
| studia niestacjonarne                                  | studia niestacjonarne                                  |
| 04.03                                                  | 16.00-18.00                                            |
| 05.03                                                  | 10.00-11.00                                            |
+--------------------------------------------------------+--------------------------------------------------------+

huangapple
  • 本文由 发表于 2023年3月20日 23:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定