python-3.x - 没有使用 python beautifulsoup 获取包含公司信息的嵌套表
问题描述
代码 :
from bs4 import BeautifulSoup
import requests
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'}
r = requests.get('http://indianindustriesdirectory.com/viewCompanyDetails.asp?compid=3855',headers=headers)
soup = BeautifulSoup(r.text,'lxml')
table = soup.find('table',{"width":"99%","cellpadding":"0","cellspacing":"0","border":"0"})
contact_person = table.find('b')
print(contact_person)
想要关于公司的所有信息,但我得到了桌子。
解决方案
您所指的网址有几个看起来几乎相同的表格,所以我随机选择了其中一个(table[5]
在本例中为 )。这段代码 - 它使用 pandas,而不是 BeautifulSoup,应该为您提供一个看起来像页面上的表格:
import pandas as pd
all_tables = pd.read_html("http://indianindustriesdirectory.com/viewCompanyDetails.asp?compid=3855")
my_table = all_tables[5][[0,2]]
my_table.drop(my_table.tail(5).index,inplace=False).dropna(axis=0, how='all')
推荐阅读
- c# - 我无法再次访问在“调用”方法中获取的数据
- javascript - 从文本框或 Jquery 自动完成菜单提交数据
- javascript - 运行有关 Require Stack - Angular 9 的 ng 服务器时出错
- c# - 将 SQL 命令转换为 Linq Lambda 函数?
- firebase - Firestore 不允许我指定数字文档 ID?它是如何工作的?
- r - 将数据从xlsx文件导入R时如何处理日期
- c# - '找不到类型或命名空间名称'如何在 Unity 中解决
- vb6 - vb6 / 通过 api 或其他方式在同一浏览器选项卡中打开 url?
- javascript - 在原始视图中以横向或纵向显示图像不拉伸
- c - 在 C 中创建一个要写入的新文件并将其初始化为空?