首页 > 解决方案 > 网络抓取表格到列表

问题描述

我正在尝试从网页中提取表格。我已经设法将表中的所有数据放入一个列表中。但是,所有表格数据都被放入一个列表元素中。我需要帮助将表格行中的“干净”数据(即字符串,没有所有 HTML 包装)放入它们自己的列表元素中。

所以,而不是...

list  = [<tr>
         <th><a href="/7.62x25mm_TT_AKBS" title="7.62x25mm TT AKBS"><img alt="TTAKBS.png" decoding="async" height="64" src="https://static.wikia.nocookie.net/escapefromtarkov_gamepedia/images/6/61/TTAKBS.png/revision/latest/scale-to-width-down/64?cb=20190519001904" width="64"/></a>
         </th>
         <th><a href="/7.62x25mm_TT_AKBS" title="7.62x25mm TT AKBS">7.62x25mm TT AKBS</a>
         </th>
         <td>58
         </td>
         <td>12
         </td>
         <td>32]

我想...

list  = ['href="/7.62x25mm_TT_AKBS" title="7.62x25mm TT AKBS"><img alt="TTAKBS.png" decoding="async" height="64" src="https://static.wikia.nocookie.net/escapefromtarkov_gamepedia/images/6/61/TTAKBS.png/revision/latest/scale-to-width-down/64?cb=20190519001904" width="64"',
         'href="/7.62x25mm_TT_AKBS" title="7.62x25mm TT AKBS">7.62x25mm TT AKBS',
         '58',
         '12',
         '32']

我的代码list可以使用以下代码进行复制。

#Import Modules
import re
import requests
from bs4 import BeautifulSoup

#Get page
cartridge_url = 'https://escapefromtarkov.gamepedia.com/7.62x25mm_Tokarev'
cartridge_page = requests.get(cartridge_url)
cartridge_soup = BeautifulSoup(cartridge_page.content, 'html.parser')

#This gets the rows of the table I want
list = cartridge_soup.find_all(lambda t: t.name =='tr')

#This gets rid of an element which is not useful
list = [n for n in dirty_temp_type if not 'class="va-navbox' in str(n)]

#I had hoped this might assemble a list..  
list = [str(n) for n in list]

我正在学习 python,我想我掌握了 HTML,但我无法让 python 与我的bs4.element.ResultSet. 我知道这不是一个复杂的解决方案,但在尝试了多种不同的方法后我遇到了障碍。我的“真正”最终目标是如下列表......

list  = ['7.62x25mm_TT_AKBS',
         '58',
         '12',
         '32']

尝试实施建议的解决方案:

---> 正如AzyCrw4282所建议的那样

顺便说一句,这是一个令人难以置信的用户名。

(一世)

我 [认为我] 可以大致了解我应该做什么,但我未能正确实施。

使用...

cartridge_table = cartridge_soup.find_all('table')

我得到了以 HTML 格式存储的所有正确数据cartridge_table。然而,跑步...

for row in cartridge_table.find_all("tr")[:1]:
    print([cell.get_text(strip=True) for cell in row.find_all("td")])

...返回...

ResultSet object has no attribute 'find_all'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

...替换find_allfind并不能解决问题。

(二)

我半信半疑地跑了……

for row in cartridge_soup.find_all("tr")[:1]:
    print([cell.get_text(strip=True) for cell in row.find_all("td")])

...但这会返回一个空列表。

您最初链接到的问题定义了一个在用必要数据header填充变量之前调用的table变量......

header = soup.find("b", text="Payable")
table = header.find_parent("table")

我不知道用什么来代替“应付账款”来让它为我工作。

(四)

我试图通过给它一个刺来否定(iii)中的上述问题......

cartridge_table = cartridge_soup.find_parent("table")

for row in cartridge_soup.find_all("tr")[:1]:
    print([cell.get_text(strip=True) for cell in row.find_all("td")])

但它返回一个空列表。当我检查时,这是因为cartridge_table变量下没有存储任何内容。

(五)

我试过跑步...

header = cartridge_soup.find("b", text="Payable")

...并用"Payable"各种看似明智的替代方案代替,看看会发生什么,但我一无所获。最终,header变量似乎总是为空。

例子:"Icon", "Name", "Fragmentation Chance", "wikitable sortable", "7.62x25mm TT LRN", "7.62x25mm_TT_AKBS".

标签: pythonweb-scrapingbeautifulsouppython-requests

解决方案


据我了解,没有办法解析这个

list  = [<tr>
         <th><a href="/7.62x25mm_TT_AKBS" title="7.62x25mm TT AKBS"><img alt="TTAKBS.png" decoding="async" height="64" src="https://static.wikia.nocookie.net/escapefromtarkov_gamepedia/images/6/61/TTAKBS.png/revision/latest/scale-to-width-down/64?cb=20190519001904" width="64"/></a>
         </th>
         <th><a href="/7.62x25mm_TT_AKBS" title="7.62x25mm TT AKBS">7.62x25mm TT AKBS</a>
         </th>
         <td>58
         </td>
         <td>12
         </td>
         <td>32]

到您想要的格式。可能有一种方法可以使用regex,但这会使它过于复杂。

这个问题虽然可以使用其他方法解决。此外,由于您尚未定义什么dirty_temp_type是[这是变量命名中的错误,现已更正],因此无法在本地调试您的代码。另外(正如评论中已经提到的),不要list用作变量名,因为这会调用的内置定义list并可能导致错误。

这里有一个完美的答案 - Python+BeautifulSoup: 从网页中抓取特定的表格- 这准确地向您展示了您需要做什么。

来自上述链接的代码片段

for row in table.find_all("tr")[:1]:
    print([cell.get_text(strip=True) for cell in row.find_all("td")])

使用它将从第一行的单元格中获取数据,并将在列表中为您提供最终目标的输出。


推荐阅读