一般情况下,对Web抓取和编码来说都是新手.对于更有经验的人来说,这可能是一个简单的问题.也许不是...它是这样的:

试图通过网络从维基百科上刮一张桌子.我已经在html中找到了表,并将该信息添加到我的代码中.但是,当我运行它时,返回的是‘None’,而不是确认表已被正确定位.

from bs4 import BeautifulSoup
from urllib.request import urlopen


url = 'https://en.wikipedia.org/wiki/List_of_songs_recorded_by_the_Beatles'
html = urlopen(url) 
soup = BeautifulSoup(html, 'html.parser')            

table = soup.find('table',{'class':'wikitable sortable plainrowheaders jquery-tablesorter'})
print(table)

返回:无

推荐答案

从"CLASS"字符串中go 掉jquery-tablesorter--这个类是由javascript添加的,beautifulsoup 看不到它(注意:一定要注意服务器发送给你的真实的HTML文档,这就是beautifulsoup 看到的东西--在浏览器中按ctrl-U键):

from urllib.request import urlopen

from bs4 import BeautifulSoup

url = "https://en.wikipedia.org/wiki/List_of_songs_recorded_by_the_Beatles"
html = urlopen(url)
soup = BeautifulSoup(html, "html.parser")

table = soup.find("table", {"class": "wikitable sortable plainrowheaders"})
print(table)

打印:

<table class="wikitable sortable plainrowheaders" style="text-align:center">
<caption>Name of song, core catalogue release, songwriter, lead vocalist and year of original release
</caption>
<tbody><tr>
<th scope="col">Song
</th>
<th scope="col">Core catalogue release(s)
</th>
<th scope="col">Songwriter(s)
</th>

...

Python相关问答推荐

使用FASTCGI在IIS上运行Django频道

比较两个数据帧并并排附加结果(获取性能警告)

查找两极rame中组之间的所有差异

按顺序合并2个词典列表

如何在Python中获取`Genericums`超级类型?

什么是合并两个embrame的最佳方法,其中一个有日期范围,另一个有日期没有任何共享列?

在Python中计算连续天数

在极中解析带有数字和SI前缀的字符串

如何检测鼠标/键盘的空闲时间,而不是其他输入设备?

ModuleNotFoundError:没有模块名为x时try 运行我的代码''

当单元测试失败时,是否有一个惯例会抛出许多类似的错误消息?

提取最内层嵌套链接

如何使用加速广播主进程张量?

如何使用大量常量优化代码?

极柱内丢失类型信息""

如何从数据框列中提取特定部分并将该值填充到其他列中?

如何在Quarto中的标题页之前创建序言页

我可以同时更改多个图像吗?

ValueError:必须在Pandas 中生成聚合值

对列中的数字进行迭代,得到n次重复开始的第一个行号