Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Wikipedia页面中删除表数据_Python_Html - Fatal编程技术网

Python 从Wikipedia页面中删除表数据

Python 从Wikipedia页面中删除表数据,python,html,Python,Html,我正在学习如何将BeautifulSoup库与Python结合使用,为了练习,我尝试从这个Wikipedia页面中删除体裁标题: 我已经能够在我的代码中实现这一点: from bs4 import BeautifulSoup html = open("wiki-jazz.html", encoding="utf=8") soup = BeautifulSoup(html, "html.parser") table = soup.f

我正在学习如何将BeautifulSoup库与Python结合使用,为了练习,我尝试从这个Wikipedia页面中删除体裁标题:

我已经能够在我的代码中实现这一点:

from bs4 import BeautifulSoup

html = open("wiki-jazz.html", encoding="utf=8")
soup = BeautifulSoup(html, "html.parser")

table = soup.find_all("table")[1]
td = table.find_all("td")
print(td)
表[1]包含我要访问的数据。更具体地说,我只需要位于此标题属性中的数据:

</td>, <td><a href="/wiki/West_Coast_jazz" title="West Coast jazz">West Coast jazz</a>
我一直在绞尽脑汁研究如何提取这些信息。我已经看过这里的其他帖子了,但还不能完全达到那里。
谢谢。

要打印表格的第一列,您可以迭代行(
),然后获取行的所有单元格(
)。每行的第一个单元格是您的爵士乐流派:

import requests
from bs4 import BeautifulSoup

url = 'https://en.wikipedia.org/wiki/List_of_jazz_genres'
soup = BeautifulSoup(requests.get(url).content, "html.parser")

table = soup.find_all("table")[1]

for row in table.find_all('tr')[1:]:    # <-- [1:] because we don't want the header
    cells = [td.get_text(strip=True) for td in row.find_all('td')]
    print(cells[0])

要打印表的第一列,您可以迭代行(
),然后获取行的所有单元格(
)。每行的第一个单元格是您的爵士乐流派:

import requests
from bs4 import BeautifulSoup

url = 'https://en.wikipedia.org/wiki/List_of_jazz_genres'
soup = BeautifulSoup(requests.get(url).content, "html.parser")

table = soup.find_all("table")[1]

for row in table.find_all('tr')[1:]:    # <-- [1:] because we don't want the header
    cells = [td.get_text(strip=True) for td in row.find_all('td')]
    print(cells[0])

您应该阅读Beautifulsoup文档,了解如何在诸如href src等标记中获取属性

在这里你可以使用

item[1].get(‘title’)

您应该阅读Beautifulsoup文档,了解如何在诸如href src等标记中获取属性

在这里你可以使用

item[1].get(‘title’)

谢谢你的帮助。使用单元格行,您创建了一个列表,对吗?如果是这样的话,它如何将单元格[0]不仅仅打印第一个索引值?我要学习这个代码。@Connor是的,
cells=[…]
被称为列表理解,它创建了新的列表。要从该列表中获取第一个值,只需调用
单元格[0]
谢谢您的帮助。使用单元格行,您创建了一个列表,对吗?如果是这样的话,它如何将单元格[0]不仅仅打印第一个索引值?我要学习这个代码。@Connor是的,
cells=[…]
被称为列表理解,它创建了新的列表。要从该列表中获取第一个值,只需调用
单元格[0]