【问题标题】:Get data from Multiple tags BeautifulSoup/Python从多个标签中获取数据 BeautifulSoup/Python
【发布时间】:2020-10-21 23:47:46
【问题描述】:

我正在尝试从该站点获取尽可能多的数据,包括标题、时间和每次事件。

例如,对于 Aniversary Awards Ceremony,它应该打印标题、8:30,以及在 8:30 发生的事件的名称。

标题后的标签(例如时间)开始重复/更改多次,无法准确拉动。有没有更好的方法来解决这个问题?将所有数据尽可能准确地提取到网站?

谢谢

import requests
from bs4 import BeautifulSoup
import pandas as pd
productlinks=[]

url='https://www.sitcancer.org/2020/program/annual-meeting-schedule-2020'
r=requests.get(url)
soup=BeautifulSoup(r.content,'html.parser')
productlist=soup.find_all('div',class_='HtmlContent')
for section in productlist:
    title=section.find('span',style="font-family: helvetica; color: #ef4136;")
    if title is not None:
        title=title.text
    else:
        title='No'
    print(title)

【问题讨论】:

  • 问题是什么?是否有某些特定的东西没有按您认为的那样工作?
  • 标题后的标签(如时间)开始重复/更改多次,无法准确拉取。有没有更好的方法来解决这个问题?将所有数据尽可能准确地提取到网站?

标签: python html web-scraping beautifulsoup


【解决方案1】:

这应该让你足够接近你需要的东西,你可以从那里拿走:

from bs4 import Tag, NavigableString
tabs = soup.select('div.HtmlContent table[style="height: 740px; width: 100%;"] tbody')
for t in tabs[0]:    
    if not isinstance(t, NavigableString ) and len(t.text.strip())>0:        
        if 'Session' in t.text:
            print(t.text)
        else:
            if t.select_one('h3') is not None:
                print(t.text.strip())
            else:                
                for w in t.select('td span strong'):
                    print(w.text.strip())
                for f in t.select('span[style="color: #000000;"]'):
                    print(f.text.strip())  

    

输出:

9 a.m.–3:15 p.m. EST* 
*Dates, times and program schedule subject to change. 


Immunotherapy Resistance and Failure


Session I: Defining Immune Checkpoint Inhibitor Resistance 


9 a.m.
Introduction

9:05 a.m.
Definitions of Resistance and Gaps in Current Understanding
Ryan J. Sullivan, MD - Massachusetts General Hospital

等等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2014-01-06
    • 2020-01-07
    相关资源
    最近更新 更多