這里是一個完全的 BeautifulSoup 新手。我需要從某個 URL 中抓取發布選項卡的內容(在下面的代碼示例中列出)。我需要抓取出版物并將它們拆分為“作者”、“標題”和“期刊”,然后我可以將它們轉換為 pandas DataFrame。我嘗試使用以下代碼抓取內容:
page_url = 'https://www.vet.upenn.edu/research/centers-laboratories/research-laboratory/research-laboratory/anguera-laboratory'
def get_soup(url):
r = requests.get('http://localhost:8050/render.html', params={'url': url, 'wait': 3})
soup = BeautifulSoup(r.text, 'html.parser')
url_soup = get_soup(page_url)
publis = url_soup.find_all('div',{'class':'tab-content pad'})
for item in publis:
publi = {
'items': item.find('div',{'id':'tabs-publications'}).text.split('\n\n')
}
publication_df = pd.DataFrame(publi)
new_publication_df = publication_df['items'].str.split(':', 1, expand=True)
new_publication_df[[1,2]] = new_publication_df[1].str.split(']',expand=True)
這會回傳我需要的內容,但它確實很容易出現拼寫錯誤(例如,有一份出版物使用“J”而不是“]”)。BeautifulSoup有什么辦法可以自動將文本分成三列?
uj5u.com熱心網友回復:
認為您應該使用可用的結構而不是拆分字串。
選擇所有<em>包含標題的,它的previous_sibling,包含作者的,還有它的next_sibling,包含期刊的。
例子
import requests
from bs4 import BeautifulSoup
r = requests.get('https://www.vet.upenn.edu/research/centers-laboratories/research-laboratory/research-laboratory/anguera-laboratory')
soup = BeautifulSoup(r.text)
data = []
for e in soup.select('#tabs-publications em'):
data.append({
'author':e.previous.get_text(strip=True)[:-1],
'title':e.get_text(strip=True),
'journal':e.next_sibling.get_text(strip=True)
})
data
輸出資料
[{'author': 'Hantsoo Liisa, Kornfield Sara, Anguera Montserrat C, Epperson C Neill',
'title': 'Inflammation: A Proposed Intermediary Between\xa0Maternal Stress and Offspring Neuropsychiatric Risk. [PMID30314641]',
'journal': 'Biological psychiatry 85(2): 97-106, Jan 2019.'},
{'author': 'Sierra Isabel, Anguera Montserrat C',
'title': 'Enjoy the silence: X-chromosome inactivation diversity in somatic cells.[PMID31108425]',
'journal': 'Current opinion in genetics & development 55: 26-31, May 2019.'},
{'author': 'Syrett Camille M, Anguera Montserrat C',
'title': 'When the balance is broken: X-linked gene dosage from two X chromosomes and female-biased autoimmunity. [PMID31125996]',
'journal': 'Journal of leukocyte biology May 2019.'},
{'author': 'Kotzin Jonathan J, Iseka Fany, Wright Jasmine, Basavappa Megha G, Clark Megan L, Ali Mohammed-Alkhatim, Abdel-Hakeem Mohamed S, Robertson Tanner F, Mowel Walter K, Joannas Leonel, Neal Vanessa D, Spencer Sean P, Syrett Camille M, Anguera Montserrat C, Williams Adam, Wherry E John, Henao-Mejia Jorge',
'title': 'The long noncoding RNA regulates CD8 T cells in response to viral infection.[PMID31138702]',
'journal': 'Proceedings of the National Academy of Sciences of the United States of America 116(24): 11916-11925, Jun 2019.'},
{'author': 'Syrett Camille M, Paneru Bam, Sandoval-Heglund Donavon, Wang Jianle, Banerjee Sarmistha, Sindhava Vishal, Behrens Edward M, Atchison Michael, Anguera Montserrat C',
'title': 'Altered X-chromosome inactivation in T cells may promote sex-biased autoimmune diseases. [PMID30944248',
'journal': 'JCI insight 4(7), Apr 2019.'},...]
輸出資料框
pd.DataFrame(data)
| 作者 | 標題 | 雜志 |
|---|---|---|
| Hantsoo Liisa, Kornfield Sara, Anguera Montserrat C, Epperson C Neill | 炎癥:母親壓力和后代神經精神風險之間的建議中介。[PMID30314641] | 生物精神病學 85(2):97-106,2019 年 1 月。 |
| 塞拉伊莎貝爾,安格拉蒙特塞拉特 C | 享受沉默:體細胞中 X 染色體失活的多樣性。[PMID31108425] | 遺傳學與發育的當前觀點 55:26-31,2019 年 5 月。 |
| Syrett Camille M,安格拉蒙特塞拉特 C | 當平衡被打破時:來自兩條 X 染色體的 X 連鎖基因劑量和偏向于女性的自身免疫。[PMID31125996] | 白細胞生物學雜志 2019 年 5 月。 |
| Kotzin Jonathan J, Iseka Fany, Wright Jasmine, Basavappa Megha G, Clark Megan L, Ali Mohammed-Alkhatim, Abdel-Hakeem Mohamed S, Robertson Tanner F, Mowel Walter K, Joannas Leonel, Neal Vanessa D, Spencer Sean P, Syrett Camille M, Anguera Montserrat C, 威廉姆斯亞當, Wherry E John, Henao-Mejia Jorge | 長鏈非編碼 RNA 調節 CD8 T 細胞以回應病毒感染。[PMID31138702] | 美國國家科學院院刊 116(24): 11916-11925,2019 年 6 月。 |
| Syrett Camille M, Paneru Bam, Sandoval-Heglund Donavon, Wang Jianle, Banerjee Sarmistha, Sindhava Vishal, Behrens Edward M, Atchison Michael, Anguera Montserrat C | T細胞中改變的X染色體失活可能會促進性別偏見的自身免疫性疾病。[PMID30944248 | JCI 洞察 4(7),2019 年 4 月。 |
| Syrett Camille M, Sindhava Vishal, Sierra Isabel, Dubin Aimee H, Atchison Michael, Anguera Montserrat C | NK 細胞、樹突細胞和巨噬細胞中無活性 X 染色體的表觀遺傳特征的多樣性。[PMID30671059] | 免疫學前沿 9:3087,2018。 |
| Le Coz Carole, Trofa Melissa, Syrett Camille M, Martin Anna, Jyonouchi Harumi, Jyonouchi Soma, Anguera Montserrat C, Romberg Neil | CD40LG 復制相關的自身免疫性疾病被非隨機 X 染色體失活所抑制。[PMID29499223] | 過敏和臨床免疫學雜志 141(6): 2308-2311.e7,2018 年 6 月。 |
| Syrett Camille M、Sierra Isabel、Berry Corbett L、Beiting Daniel、Anguera Montserrat C | 在人類胚胎干細胞和人類胎盤祖細胞的體外分化程序中,性別特異性基因表達差異是顯而易見的。[PMID29993333] | 干細胞與發育 27(19):1360-1375,2018 年 10 月。 |
| 王建樂,安格拉·蒙特塞拉特 C | 人多能干細胞體外分化為滋養層細胞。[PMID28362386] | 可視化實驗雜志:JoVE(121),2017 年 3 月。 |
| Syrett Camille M, Sindhava Vishal, Hodawadekar Suchita, Myles Arpita, 梁冠祥, 張悅, Nandi Satabdi, Cancro Michael, Atchison Michael, Anguera Montserrat C | 在 B 細胞發育程序中,來自非活性 X 的 Xist RNA 損失在活化的 B 細胞中以動態 YY1 依賴性兩步程序恢復。[PMID28991910] | PLoS 遺傳學 13(10):e1007050,2017 年 10 月。 |
| Penkala Ian, Wang Jianle, Syrett Camille M, Goetzl Laura, López Carolina B, Anguera Montserrat C | lncRHOXF1,一種來自 X 染色體的長鏈非編碼 RNA,在早期人類胎盤發育程序中抑制病毒反應基因。[PMID27066803] | 分子和細胞生物學 36(12):1764-75,2016 年 6 月。 |
| 王建樂 Syrett Camille M Kramer Marianne C Basu Arindam Atchison Michael L Anguera Montserrat C | X 染色體失活的例外維持使雌性淋巴細胞易于從失活的 X 中增加表達。 [PMID27001848] | 美國國家科學院院刊 113(14): E2029-38,2016 年 4 月。 |
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/486883.html
