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

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

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:

  1. import requests
  2. from bs4 import BeautifulSoup
  3. # Make a GET request to the URL
  4. url = "https://we.umg.edu.pl/ktm/konsultacje"
  5. response = requests.get(url)
  6. soup = BeautifulSoup(response.content, 'html.parser')
  7. roomsProfessors = soup.find_all('h3')
  8. # Lists for sorted data
  9. professors = []
  10. rooms = []
  11. # Get data about rooms and professors and fill their lists
  12. for roomProfessor in roomsProfessors:
  13. professor = []
  14. for child in roomProfessor:
  15. if child.name == 'br':
  16. continue
  17. child = child.string.replace('\t', '').replace('\n', '')
  18. if 'pok.' in child:
  19. rooms.append(child)
  20. else:
  21. professor.append(child)
  22. 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

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

  1. Not sure what your expected output should look like, but you could iterate with `find_next_siblings()` or `previous_siblings()`
  2. import requests
  3. from bs4 import BeautifulSoup
  4. url = "https://we.umg.edu.pl/ktm/konsultacje"
  5. response = requests.get(url)
  6. soup = BeautifulSoup(response.content, 'html.parser')
  7. data = []
  8. for p in soup.select('tr:has(h3)'):
  9. d ={
  10. 'prof': p.h3.contents[0].strip(),
  11. 'room': p.h3.contents[-1].strip(),
  12. 'times': []
  13. }
  14. for e in p.find_next_siblings('tr'):
  15. if e.h3:
  16. break
  17. if len(e.text.strip()) > 1 and not e.h5:
  18. d['times'].append(e.get_text('|',strip=True))
  19. data.append(d)
  20. data

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

  1. 不确定您期望的输出应该是什么但您可以使用`find_next_siblings()``previous_siblings()`进行迭代
  2. import requests
  3. from bs4 import BeautifulSoup
  4. url = "https://we.umg.edu.pl/ktm/konsultacje"
  5. response = requests.get(url)
  6. soup = BeautifulSoup(response.content, 'html.parser')
  7. data = []
  8. for p in soup.select('tr:has(h3)'):
  9. d ={
  10. 'prof': p.h3.contents[0].strip(),
  11. 'room': p.h3.contents[-1].strip(),
  12. 'times': []
  13. }
  14. for e in p.find_next_siblings('tr'):
  15. if e.h3:
  16. break
  17. if len(e.text.strip()) > 1 and not e.h5:
  18. d['times'].append(e.get_text('|',strip=True))
  19. data.append(d)
  20. data
英文:

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

Example
  1. import requests
  2. from bs4 import BeautifulSoup
  3. url = "https://we.umg.edu.pl/ktm/konsultacje"
  4. response = requests.get(url)
  5. soup = BeautifulSoup(response.content, 'html.parser')
  6. data = []
  7. for p in soup.select('tr:has(h3)'):
  8. d ={
  9. 'prof': p.h3.contents[0].strip(),
  10. 'room': p.h3.contents[-1].strip(),
  11. 'times': []
  12. }
  13. for e in p.find_next_siblings('tr'):
  14. if e.h3:
  15. break
  16. if len(e.text.strip()) > 1 and not e.h5:
  17. d['times'].append(e.get_text('|',strip=True))
  18. data.append(d)
  19. data
Output
  1. [{'prof': 'dr hab. inż. Andrzej Borys, prof. UMG',
  2. 'room': 'pok. C28',
  3. 'times': ['czwartek|12.00-14.00',
  4. '11.03|09.00-10.00',
  5. '25.03|09.00-10.00',
  6. '15.04|09.00-10.00',
  7. '29.04|09.00-10.00',
  8. '10.06|10.00-11.00']},
  9. {'prof': 'mgr inż. Dawid Budnarowski',
  10. 'room': 'pok. C-51',
  11. 'times': ['wtorekroda|11.00-12.00|11.00-12.00']},
  12. {'prof': 'dr inż. Tomasz Ciszewski',
  13. 'room': 'pok. C345',
  14. 'times': [&#39roda|15.00-17.00']},
  15. {'prof': 'dr inż. Wiesław Citko',
  16. 'room': 'pok. C341',
  17. 'times': ['czwartek|9.00-11.00',
  18. '04.03|11.00-12.00',
  19. '01.04|10.00-11.00',
  20. '20.05|12.00-13.00',
  21. '16.06|19.00-20.00',
  22. '07.07|16.00-17.00']},...]

答案2

得分: 1

以下是使用pandas的代码:

  1. import pandas as pd
  2. import requests
  3. from tabulate import tabulate
  4. konsultacje = "https://we.umg.edu.pl/ktm/konsultacje"
  5. df = (
  6. pd
  7. .read_html(requests.get(konsultacje).text, flavor="lxml")[0]
  8. .dropna(axis=0, how="all")
  9. )
  10. df.columns = ["Kolumna 1", "Kolumna 2"]
  11. df = df[["Kolumna 1", "Kolumna 2"]]
  12. print(tabulate(df, headers="keys", tablefmt="psql", showindex=False))

输出:

  1. +--------------------------------------------------------+--------------------------------------------------------+
  2. | Kolumna 1 | Kolumna 2 |
  3. |--------------------------------------------------------+--------------------------------------------------------|
  4. | dr hab. inż. Andrzej Borys, prof. UMG pok. C28 | dr hab. inż. Andrzej Borys, prof. UMG pok. C28 |
  5. | studia stacjonarne | studia stacjonarne |
  6. | czwartek | 12.00-14.00 |
  7. | studia niestacjonarne | studia niestacjonarne |
  8. | 11.03 | 09.00-10.00 |
  9. | 25.03 | 09.00-10.00 |
  10. | 15.04 | 09.00-10.00 |
  11. | 29.04 | 09.00-10.00 |
  12. | 10.06 | 10.00-11.00 |
  13. | mgr inż. Dawid Budnarowski pok. C-51 | mgr inż. Dawid Budnarowski pok. C-51 |
  14. | studia stacjonarne | studia stacjonarne |
  15. | wtorek środa | 11.00-12.00 11.00-12.00 |
  16. | dr inż. Tomasz Ciszewski pok. C345 | dr inż. Tomasz Ciszewski pok. C345 |
  17. | studia stacjonarne | studia stacjonarne |
  18. | środa | 15.00-17.00 |
  19. | dr inż. Wiesław Citko pok. C341 | dr inż. Wiesław Citko pok. C341 |
  20. | studia stacjonarne | studia stacjonarne |
  21. | czwartek | 9.00-11.00 |
  22. | studia niestacjonarne | studia niestacjonarne |
  23. | 04.03 | 11.00-12.00 |
  24. | 01.04 | 10.00-11.00 |
  25. | 20.05 | 12.00-13.00 |
  26. | 16.06 | 19.00-20.00 |
  27. | 07.07 | 16.00-17.00 |
  28. | dr inż. Piotr Kaczorek pok. C341 | dr inż. Piotr Kaczorek pok. C341 |
  29. | studia stacjonarne | studia stacjonarne |
  30. | czwartek | 11.00-13.00 |
  31. | studia niestacjonarne | studia niestacjonarne |
  32. | 18.03 | 11.00-12.00 |
  33. | 23.04 | 19.00-20.00 |
  34. | 13.05 | 13.00-14.00 |
  35. | 10.06 | 12.00-13.00 |
  36. | dr inż. Karol Korcz, prof. UMG pok. C349 | dr inż. Karol Korcz, prof. UMG pok. C349 |
  37. | studia stacjonarne | studia stacjonarne |
  38. | czwartek | 11.00-13.00 |
  39. | studia niestacjonarne | studia niestacjonarne |
  40. | 05.03 | 13.00-14.00 |
  41. | 19.03 | 13.00-14.00 |
  42. | 23.04 | 14.00-15.00 |
  43. | 07.05 | 12.00-13.00 |
  44. | 03.06 | 14.00-16.00 |
  45. | dr inż. Ewa Krac pok. C51 | dr inż. Ewa Krac pok. C51 |
  46. | studia stacjonarne | studia stacjonarne |
  47. | środa piątek | 10.30-11.30 10.30-11.30 |
  48. | studia niestacjonarne | studia niestacjonarne |
  49. | 19.03 | 13.00-14.00 |
  50. | 23.04 | 13.00-14.00 |
  51. | 04.06 | 14.00.15.00 |
  52. | dr inż. Stanisław Lindner pok. C344 | dr inż. Stanisław Lindner pok. C344 |
  53. | studia stacjonarne | studia stacjonarne |
  54. | czwartek | 13.00-15.00 |
  55. | studia niestacjonarne | studia niestacjonarne |
  56. | 19.03 | 12.00-13.00 |
  57. | 22.04 | 10.00-11.00 |
  58. | 06.05 | 11.00-12.00 |
  59. | 04.06 | 11.00-12.00 |
  60. | 18.06 | 11.00-12.00 |
  61. | dr inż. Andrzej Łuksza pok. C341 | dr inż. Andrzej Łuksza pok. C341 |
  62. | studia stacjonarne | studia stacjonarne |
  63. | środa | 11.00-13.00 |
  64. | studia niestacjonarne | studia niestacjonarne |
  65. | 19.03 | 13.00-14.00 |
  66. | 23.04 | 13.00-14.00 |
  67. | 06.05 | 14.00-15.00 |
  68. | 17.06 | 11.00-12.00 |
  69. | dr inż. Beata Pałczyńska
  70. <details>
  71. <summary>英文:</summary>
  72. You&#39;re not saying what your expected output should be, but here&#39;s a brute force approach with `pandas`.
  73. ```python
  74. import pandas as pd
  75. import requests
  76. from tabulate import tabulate
  77. konsultacje = &quot;https://we.umg.edu.pl/ktm/konsultacje&quot;
  78. df = (
  79. pd
  80. .read_html(requests.get(konsultacje).text, flavor=&quot;lxml&quot;)[0]
  81. .dropna(axis=0, how=&quot;all&quot;)
  82. )
  83. df.columns = [&quot;Kolumna 1&quot;, &quot;Kolumna 2&quot;]
  84. df = df[[&quot;Kolumna 1&quot;, &quot;Kolumna 2&quot;]]
  85. print(tabulate(df, headers=&quot;keys&quot;, tablefmt=&quot;psql&quot;, showindex=False))

Output:

  1. +--------------------------------------------------------+--------------------------------------------------------+
  2. | Kolumna 1 | Kolumna 2 |
  3. |--------------------------------------------------------+--------------------------------------------------------|
  4. | dr hab. inż. Andrzej Borys, prof. UMG pok. C28 | dr hab. inż. Andrzej Borys, prof. UMG pok. C28 |
  5. | studia stacjonarne | studia stacjonarne |
  6. | czwartek | 12.00-14.00 |
  7. | studia niestacjonarne | studia niestacjonarne |
  8. | 11.03 | 09.00-10.00 |
  9. | 25.03 | 09.00-10.00 |
  10. | 15.04 | 09.00-10.00 |
  11. | 29.04 | 09.00-10.00 |
  12. | 10.06 | 10.00-11.00 |
  13. | mgr inż. Dawid Budnarowski pok. C-51 | mgr inż. Dawid Budnarowski pok. C-51 |
  14. | studia stacjonarne | studia stacjonarne |
  15. | wtorek środa | 11.00-12.00 11.00-12.00 |
  16. | dr inż. Tomasz Ciszewski pok. C345 | dr inż. Tomasz Ciszewski pok. C345 |
  17. | studia stacjonarne | studia stacjonarne |
  18. | środa | 15.00-17.00 |
  19. | dr inż. Wiesław Citko pok. C341 | dr inż. Wiesław Citko pok. C341 |
  20. | studia stacjonarne | studia stacjonarne |
  21. | czwartek | 9.00-11.00 |
  22. | studia niestacjonarne | studia niestacjonarne |
  23. | 04.03 | 11.00-12.00 |
  24. | 01.04 | 10.00-11.00 |
  25. | 20.05 | 12.00-13.00 |
  26. | 16.06 | 19.00-20.00 |
  27. | 07.07 | 16.00-17.00 |
  28. | dr inż. Piotr Kaczorek pok. C341 | dr inż. Piotr Kaczorek pok. C341 |
  29. | studia stacjonarne | studia stacjonarne |
  30. | czwartek | 11.00-13.00 |
  31. | studia niestacjonarne | studia niestacjonarne |
  32. | 18.03 | 11.00-12.00 |
  33. | 23.04 | 19.00-20.00 |
  34. | 13.05 | 13.00-14.00 |
  35. | 10.06 | 12.00-13.00 |
  36. | dr inż. Karol Korcz, prof. UMG pok. C349 | dr inż. Karol Korcz, prof. UMG pok. C349 |
  37. | studia stacjonarne | studia stacjonarne |
  38. | czwartek | 11.00-13.00 |
  39. | studia niestacjonarne | studia niestacjonarne |
  40. | 05.03 | 13.00-14.00 |
  41. | 19.03 | 13.00-14.00 |
  42. | 23.04 | 14.00-15.00 |
  43. | 07.05 | 12.00-13.00 |
  44. | 03.06 | 14.00-16.00 |
  45. | dr inż. Ewa Krac pok. C51 | dr inż. Ewa Krac pok. C51 |
  46. | studia stacjonarne | studia stacjonarne |
  47. | środa piątek | 10.30-11.30 10.30-11.30 |
  48. | studia niestacjonarne | studia niestacjonarne |
  49. | 19.03 | 13.00-14.00 |
  50. | 23.04 | 13.00-14.00 |
  51. | 04.06 | 14.00.15.00 |
  52. | dr inż. Stanisław Lindner pok. C344 | dr inż. Stanisław Lindner pok. C344 |
  53. | studia stacjonarne | studia stacjonarne |
  54. | czwartek | 13.00-15.00 |
  55. | studia niestacjonarne | studia niestacjonarne |
  56. | 19.03 | 12.00-13.00 |
  57. | 22.04 | 10.00-11.00 |
  58. | 06.05 | 11.00-12.00 |
  59. | 04.06 | 11.00-12.00 |
  60. | 18.06 | 11.00-12.00 |
  61. | dr inż. Andrzej Łuksza pok. C341 | dr inż. Andrzej Łuksza pok. C341 |
  62. | studia stacjonarne | studia stacjonarne |
  63. | środa | 11.00-13.00 |
  64. | studia niestacjonarne | studia niestacjonarne |
  65. | 19.03 | 13.00-14.00 |
  66. | 23.04 | 13.00-14.00 |
  67. | 06.05 | 14.00-15.00 |
  68. | 17.06 | 11.00-12.00 |
  69. | dr inż. Beata Pałczyńska pok. C339 | dr inż. Beata Pałczyńska pok. C339 |
  70. | studia stacjonarne | studia stacjonarne |
  71. | środa | 13.00-14.00 |
  72. | dr inż. Dorota Rabczuk pok. C339 | dr inż. Dorota Rabczuk pok. C339 |
  73. | studia stacjonarne | studia stacjonarne |
  74. | wtorek | 14.00-16.00 |
  75. | mgr inż. Wojciech Rabczuk pok. C51 | mgr inż. Wojciech Rabczuk pok. C51 |
  76. | studia stacjonarne | studia stacjonarne |
  77. | poniedziałek | 7.00-8.00, 12.00-13.00 |
  78. | studia niestacjonarne | studia niestacjonarne |
  79. | 05.03 | 07.00-08.00 |
  80. | 19.03 | 11.00-12.00 |
  81. | dr hab. inż. Agnieszka Rybarczyk, prof. UMG pok. C345 | dr hab. inż. Agnieszka Rybarczyk, prof. UMG pok. C345 |
  82. | studia stacjonarne | studia stacjonarne |
  83. | piątek | 10.00-12.00 |
  84. | dr hab. inż. Wiesław Sieńko, prof. UMG pok. C208 | dr hab. inż. Wiesław Sieńko, prof. UMG pok. C208 |
  85. | studia stacjonarne | studia stacjonarne |
  86. | czwartek | 11.00-13.00 |
  87. | studia niestacjonarne | studia niestacjonarne |
  88. | 04.03 | 14:00 - 15:00 |
  89. | 18.03 | 14:00 - 15:00 |
  90. | 01.04 | 12:00 - 13:00 |
  91. | 06.05 | 14:00 - 15:00 |
  92. | 20.05 | 15:00 - 16:00 |
  93. | 03.06 | 11:00 - 12:00 |
  94. | mgr inż. Marta Szarmach pok. C339 | mgr inż. Marta Szarmach pok. C339 |
  95. | studia stacjonarne | studia stacjonarne |
  96. | wtorek | 10.00-12.00 |
  97. | Agata Śledzińska-Płotczyk pok. C-326 | Agata Śledzińska-Płotczyk pok. C-326 |
  98. | studia stacjonarne | studia stacjonarne |
  99. | piątek | 15.30-16.30 |
  100. | mgr inż. Marcin Waraksa pok. C336 | mgr inż. Marcin Waraksa pok. C336 |
  101. | studia stacjonarne | studia stacjonarne |
  102. | wtorek | 15.00-17.00 |
  103. | studia niestacjonarne | studia niestacjonarne |
  104. | 05.03 | 14.00-15.00 |
  105. | 19.03 | 14.00-15.00 |
  106. | 02.04 | 16.00-17.00 |
  107. | 14.04 | 16.00-17.00 |
  108. | 30.04 | 13.00-14.00 |
  109. | dr hab. inż. Andrzej Żak, prof. UMG pok. C326 | dr hab. inż. Andrzej Żak, prof. UMG pok. C326 |
  110. | studia stacjonarne | studia stacjonarne |
  111. | wtorek | 16.00-18.00 |
  112. | studia niestacjonarne | studia niestacjonarne |
  113. | 04.03 | 16.00-18.00 |
  114. | 05.03 | 10.00-11.00 |
  115. +--------------------------------------------------------+--------------------------------------------------------+

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:

确定