我想在網站https://www.fed.cuhk.edu.hk/cri/faculty/prof-bai-barry/上吸引期刊資訊。
但是,當我嘗試使用以下方法時,它會吸引我不想獲得的所有資訊,例如Selected Research and Development Projects with layer。ol
for ol in soup.select(".ar-faculty-section-content ol li"):
它將回傳ol類名中的所有內容".ar-faculty-section-content"。
我期望得到的是只獲取標簽ol下的內容。<b> Refereed Journal articles </b> 我該如何處理?

uj5u.com熱心網友回復:
在示例中使用css selectors通過其文本查找元素并使用它來adjacent sibling combinator選擇:olli
for e in soup.select('b:-soup-contains("Refereed Journal articles") ol li'):
print(e.text)
例子
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0'}
r = requests.get('https://www.fed.cuhk.edu.hk/cri/faculty/prof-bai-barry/', headers=headers)
soup = BeautifulSoup(r.text)
for e in soup.select('b:-soup-contains("Refereed Journal articles") ol li'):
print(e.text)
輸出
Wang, J., & Bai, B., (2022). Whose goal emphases play a more important role in ESL/EFL learners’ motivation, self-regulated learning and achievement?: Teachers’ or parents’. Research Papers in Education, 1-22. doi:10.1080/02671522.2022.2030395.
Guo, W. J., Lau, K. L., Wei, J., & Bai, B. (2021). Academic subject and gender differences in high school students’ self-regulated learning of language and mathematics. Current Psychology, 1-16. doi: 10.1007/s12144-021-02120-9.
Guo, W. J., Bai, B, & Song, H. (2021). Influences of process-based instruction on students’ use of self-regulated learning strategies in EFL writing. System, 1-11. doi: 10.1016/j.system.2021.102578.
...
編輯
事實上,并非所有網站都<b>具有上述方法的文本Refereed Journal articles以空串列結尾。
如果目標是獲取所有出版物,則可以選擇<h5>with textSelected Publications并general sibling combinator結合 with nth-of-type():
for e in soup.select('h5:-soup-contains("Selected Publications") ~ ol:nth-of-type(1) li'):
print(e.text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483243.html
上一篇:刮痧問題。未檢索到的資料
