这里的美丽汤新手。我需要从某个 URL(在下面的代码示例中列出)中抓取发布选项卡的内容。我需要抓取出版物并将它们拆分为"作者"、"标题"和"期刊",然后我可以将其转换为熊猫数据帧。我尝试使用以下代码抓取内容:
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('nn')
}
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"而不是"]")。有没有办法让美丽汤自动将文本分成三列?
认为你应该使用可用的结构而不是拆分字符串。
选择所有保存标题、其previous_sibling
、保存作者及其next_sibling
、保存期刊的所有<em>
。
例
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 Betweenxa0Maternal 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)
author | title | |
---|---|---|
Hantsoo Liisa, Kornfield Sara, Anguera Montserrat C, Epperson C Neill | Inflammation: | 母体压力和后代神经精神风险之间的中介。[PMID30314641]生物精神病学85(2):97-106,2019年1月。 |
Sierra Isabel, Anguera MontserratC | 享受沉默:体细胞中的X染色体失活多样性。PMID31108425] | 遗传学与发育的当前观点55:26-31,2019年5月。 |
Syrett Camille M, Anguera Montserrat | 当平衡被打破时:来自两条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 MontserratC, Williams Adam, Wherry E John, Henao-Mejia 豪尔赫 | >长非编码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 MontserratC | T细胞中改变的X染色体失活可能会促进性别偏向的自身免疫性疾病。[PMID30944248 | JCI洞察4(7),2019年4月。 |
Syrett Camille M, Sindhava Vishal, Sierra Isabel, Dubin Aimee H, Atchison Michael, Anguera Montserrat | >NK细胞、树突状细胞和巨噬细胞中无活性X染色体表观遗传特征的多样性。[PMID30671059] | Frontiers in Immunology 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 MontserratC | 性别特异性基因表达差异在人类胚胎干细胞和人胎盘祖细胞体外分化过程中很明显。[PMID29993333] | 干细胞与发育27(19):1360-1375,2018年10月。 |
Wang Jianle, Anguera Montserrat | 人多能干细胞体外分化为滋养细胞。[PMID28362386] | 可视化实验杂志:JoVE(121),2017 年 3 月。 |
Syrett Camille M, Sindhava Vishal, Hodawadekar Suchita, Myles Arpita, 梁冠祥, 张悦, Nandi Satabdi, Cancro Michael, Atchison Michael, Anguera MontserratC | 在活化的B细胞中,在活化的B细胞中,在动态YY1依赖性两步过程中恢复了B细胞发育过程中非活性X的Xist RNA丢失。[PMID28991910] | PLoS 遗传学 13(10):e1007050,2017 年 10 月。 |
Penkala Ian, Wang Jianle, Syrett Camille M, Goetzl Laura, LópezCarolina B, Anguera Montserrat | lncRHOXF1,一种来自X染色体的长非编码RNA,在早期人胎盘发育过程中抑制病毒反应基因。[PMID27066803] | 分子和细胞生物学36(12):1764-75,2016年6月。 |
Wang Jianle, Syrett Camille M, Kramer Marianne C, Basu Arindam, Atchison Michael L, Anguera Montserrat | X染色体失活的异常维持使女性淋巴细胞倾向于增加非活性X的表达。 [PMID27001848] | 美国国家科学院院刊113(14):E2029-38,2016年4月。 |