이 목록을 DataFrame -Python / BeautifulSoup에 인쇄하는 방법

Oct 22 2020

이 코드의 출력은 아래 제공된 웹 사이트의 각 행을 인쇄합니다.

그러나 태그도 포함됩니다. 기본적으로 모든 행을 Excel에 넣을 수있는 dataFrame으로 인쇄하고 싶습니다.

.text는 이름에서 반복되는 태그가 있으므로 find_all을 사용하고 있기 때문에 작동하지 않습니다.

원치 않는 태그를 제거한 다음 목록을 DF에 넣어 웹 사이트를 복제하는 과정은 어떻게됩니까?

감사.

import requests
from bs4 import BeautifulSoup
import pandas as pd
productlinks=[]
r=requests.get(url)
soup= BeautifulSoup(r.content,'html.parser')
content=soup.find_all('tr')
for item in content:
    title=item.find_all('td')
    print(title)

답변

1 AndrejKesely Oct 22 2020 at 04:03

가장 쉬운 방법은 다음을 사용하는 것입니다 pandas.read_html.

import pandas as pd

url='https://sitc.sitcancer.org/2020/abstracts/titles/'
df = pd.read_html(url)[0]
print(df)
df.to_csv('data.csv', index=False)

인쇄물:

       #  ...                                           Keywords
0      1  ...  Adoptive immunotherapy; Monocyte/Macrophage; T...
1      2  ...  CAR T cells; Immune monitoring; Inflammation; ...
2      3  ...  Antibody; Biomarkers; Immune monitoring; T cel...
3      4  ...  Biomarkers; RNA; Solid tumors; Tumor microenvi...
4      5  ...  Antibody; B cell; Biomarkers; Immune monitorin...
..   ...  ...                                                ...
730  752  ...  Gene expression; Neoantigens; Regulatory T cel...
731  753  ...  Gene expression; Neoantigens; Regulatory T cel...
732  754  ...  Biomarkers; Chemokine; Chemotherapy; Costimula...
733  755  ...  Chemokine; Granulocyte; Myeloid cells; MDSC; T...
734  756  ...  Gene expression; Immune contexture; Immune sup...

[735 rows x 6 columns]

다음을 저장합니다 data.csv(LibreOffice의 스크린 샷).