Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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_Web Scraping_Beautifulsoup_Html Parsing - Fatal编程技术网

Python 如何在wikipedia页面中刮取一个页面的多个表?

Python 如何在wikipedia页面中刮取一个页面的多个表?,python,web-scraping,beautifulsoup,html-parsing,Python,Web Scraping,Beautifulsoup,Html Parsing,我正试图将下一页的所有表格拼凑成一个大表格; 但问题是,我的代码似乎只下载了Pilot表,其他的都没有。我看过html,发现它们都有相同的类,所以我如何区分它们 提前谢谢 import requests import pandas as pd from bs4 import BeautifulSoup page = requests.get("https://en.wikipedia.org/wiki/List_of_QI_episodes") soup = BeautifulSoup(pag

我正试图将下一页的所有表格拼凑成一个大表格;

但问题是,我的代码似乎只下载了Pilot表,其他的都没有。我看过html,发现它们都有相同的类,所以我如何区分它们

提前谢谢

import requests
import pandas as pd
from bs4 import BeautifulSoup
page = requests.get("https://en.wikipedia.org/wiki/List_of_QI_episodes")
soup = BeautifulSoup(page.content, "lxml")
My_table = soup.find("table",{"class":"wikitable plainrowheaders wikiepisodetable"})
print(My_table)

请尝试改用find_all,注意这将返回一个节点列表,而不仅仅是一个节点。

如果要将所有表作为目标,可以只选择符合条件的所有表。
select
将返回所有表:

import requests
from bs4 import BeautifulSoup
page = requests.get("https://en.wikipedia.org/wiki/List_of_QI_episodes")
soup = BeautifulSoup(page.content, "lxml")
print(soup.select("table.wikitable.plainrowheaders.wikiepisodetable")
如果要单独针对每个表,可以使用
:has()
选择器针对包含所需特定
id
的表前面的标题,然后是将查找下一个子项的
+
组合符。下面是一个针对其中两个表的简单示例

import requests
from bs4 import BeautifulSoup
page = requests.get("https://en.wikipedia.org/wiki/List_of_QI_episodes")
soup = BeautifulSoup(page.content, "lxml")
for tid in ('Pilot', 'Series_A_\\.282003\\.29'):
    table = soup.select_one("h3:has(span#{}) + table.wikitable.plainrowheaders.wikiepisodetable".format(tid))
    print(table)
您可以将类似的逻辑扩展到您想要的任何目标


编辑:在第二个示例中,使用
选择一个
,因为我们的目标是一个表而不是多个表。

而不是全部查找。find_all返回具有相应属性的所有节点,而find只返回第一个节点

import requests
import pandas as pd
from bs4 import BeautifulSoup
page = requests.get("https://en.wikipedia.org/wiki/List_of_QI_episodes")
soup = BeautifulSoup(page.content, "lxml")
my_tables = soup.find_all("table",{"class":"wikitable plainrowheaders wikiepisodetable"})
for table in my_tables:
    print(table)


您也可以使用熊猫阅读html

import requests
import pandas as pd

url = 'https://en.wikipedia.org/wiki/List_of_QI_episodes'
re = requests.get(url, headers =  {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'})
df = pd.read_html(re.text)
print(df)

我喜欢这个。例如,如果我在实际页面上通过devtools使用h3:has(span#Pilot),为什么不选择任何内容?Chrome版本71.0.3578.98(官方版本)(64位)您必须在
chrome://flags
(但它可能会导致某些页面出现问题,因此我不建议将其保留在打开状态)<代码>:has()
是一个仅计划用于的4级选择器。没有多少人实现了它,这有点实验性,但对于刮取来说,它可能非常有用,这就是为什么我在Soup Sieve(底层选择库)中实现了它。也有可能我对选择器感到困惑,chrome甚至在启用标志的情况下也不支持它:。我在开发过程中测试了太多,可能会弄错浏览器支持哪些:)。