python - 如何抓取网页中并排放置的两个表的数据?

标签 python python-3.x web-scraping beautifulsoup

我用 python 结合 BeautiflSoup 编写了一个脚本,使用 .select() 来解析一些 html 元素中的表格数据。实际数据在两个表之内。当我打开该网站时,我可以看到两个表格并排放置,如 you can see in this image 。我用铅笔标记了第二张 table ,让您知道它实际上是彼此靠近的第二张 table 。

但是,当我执行下面的脚本时,我得到了一个又一个的输出。我如何获取它们,就像它们在该图像中可见一样,即并排。

我无法粘贴 html 元素,因为它们太大,无法适应下面的区域。但是,可以找到 in this link 。无法共享网站链接,因为它需要登录才能访问数据。

from bs4 import BeautifulSoup

htmldoc = """replace_with_above_elements"""

soup = BeautifulSoup(htmldoc,"lxml")
for items in soup.select("tbody.Table2__tbody tr"):
    data = [item.text for item in items.select("td")]
    print(data)

顺便说一句,这两个表的结构相似。

当前输出:

['PG', 'Empty', '', '--', '']
['SG', 'Eric GordonHouSG', '', '@LAC', '7:00 AM']
['SF', 'Taurean PrinceAtlSF', '', '@Cle', '4:00 AM']
['PF', 'Empty', '', '--', '']
['C', 'Jusuf NurkicPorC', '', '--', '']
['UTIL', 'Paul GeorgeOKCSF', '', 'Sac', '5:00 AM']
['UTIL', 'Gary HarrisDenSG', '', 'GS', '6:00 AM']
['UTIL', "D'Angelo RussellBknPG, SG", '', '--', '']
['Bench', 'Damian LillardPorPG', '', '--', '']
['Bench', 'Devin BookerPhxSG', '', '--', '']
['Bench', 'Deandre AytonPhxC', '', '--', '']
['Bench', 'Mike ConleyMemPG', '', '--', '']
['Bench', 'Trevor ArizaPhxSF', '', '--', '']
['Bench', 'Serge IbakaTorPF', '', '--', '']
['IR', 'Isaiah ThomasODenPG', '', 'GS', '6:00 AM']
['--', '--/--', '--', '--/--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--']
['30.5', '5.5/13.5', '.407', '3.0/3.0', '1.000', '1.0', '2.0', '1.0', '0.0', '1.0', '0.5', '15.0', '0.82', '53.1', '+5.0']
['27.5', '8.5/15.0', '.567', '4.0/4.0', '1.000', '3.5', '5.0', '4.5', '1.5', '0.0', '5.0', '24.5', '5.76', '49.2', '+10.8']
['--', '--/--', '--', '--/--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '--']
['20.0', '6.5/13.0', '.500', '1.0/1.0', '1.000', '0.0', '8.5', '1.0', '1.5', '0.5', '2.0', '14.0', '2.05', '78.6', '-5.1']
['37.5', '8.0/25.0', '.320', '4.0/5.5', '.727', '3.5', '5.0', '4.5', '3.0', '0.0', '4.0', '23.5', '1.80', '99.8', '-0.1']
['34.5', '6.5/15.5', '.419', '5.5/6.0', '.917', '0.5', '3.5', '3.0', '1.0', '0.5', '0.5', '19.0', '3.08', '76.4', '-5.5']
['30.0', '4.3/12.3', '.351', '1.3/1.7', '.800', '1.7', '4.3', '6.0', '0.7', '0.7', '3.0', '11.7', '2.43', '81.2', '-6.3']
['32.5', '9.0/18.0', '.500', '8.0/8.0', '1.000', '2.5', '4.0', '6.5', '0.5', '0.5', '1.5', '28.5', '8.47', '99.9', '0.0']
['34.5', '8.5/17.0', '.500', '9.5/11.5', '.826', '3.5', '3.0', '7.0', '0.5', '0.0', '4.5', '30.0', '4.30', '99.7', '+0.2']
['29.5', '5.0/9.0', '.556', '1.5/2.0', '.750', '0.0', '9.0', '3.5', '0.5', '0.5', '1.0', '11.5', '2.14', '98.4', '-0.2']
['27.0', '4.5/12.0', '.375', '2.5/3.0', '.833', '2.0', '3.0', '7.0', '0.5', '0.0', '0.5', '13.5', '2.09', '92.2', '-2.6']
['33.0', '4.5/9.5', '.474', '1.0/1.0', '1.000', '3.0', '5.5', '4.0', '0.5', '0.5', '1.5', '13.0', '3.31', '40.9', '+31.2']
['29.3', '5.3/11.3', '.471', '4.0/5.7', '.706', '0.7', '7.3', '1.0', '1.0', '1.7', '1.7', '15.3', '5.29', '36.0', '+2.4']
['--', '--/--', '--', '--/--', '--', '--', '--', '--', '--', '--', '--', '--', '--', '37.1', '-17.9']

预期输出在上面的图像链接中可见。

最佳答案

您可以尝试如下处理两个表:

soup = BeautifulSoup(htmldoc,"lxml")

tables = soup.select("tbody.Table2__tbody")

for left, right in zip(tables[0].select("tr"), tables[1].select("tr")):
    data = [item.text for item in left.select("td")] + [item.text for item in right.select("td")]
    print(data)

关于python - 如何抓取网页中并排放置的两个表的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52919602/

相关文章:

Python:如何将 Shutil.copy() 与 unicode 文件名一起使用

python - 如何在 python 3.6 中键入 hinte 高阶函数?

python - 网络抓取 python <span class ="b6a29bc0"aria-label ="Beds">2</span>, <span class ="b6a29bc0"aria-label ="Baths">2</span>

python - Selenium with/Firefox 88 更改为 navigator.webdriver

python - 从产品页面内部获取产品的所有图片

python - 使用 Python 对字符串中的元素进行排序

python - 获取 Matplotlib 注释中箭头的坐标

python - "Tuple comprehensions"和 star splat/unpack 运算符 *

python - 可以使这两个 django View 变干吗?

python - 在内联 if/else 语句中分配多个值