如何访问inspect using Python和BeautifulSoup中所示的特定表以进行web刮取

如何访问inspect using Python和BeautifulSoup中所示的特定表以进行web刮取,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我正在使用Python和BeautifulSoup进行web抓取。我的目的是从中提取成员数据。大约有1685条记录 当我在Chrome上查看页面源代码时,我找不到表。似乎它动态地提取数据。但是当我使用Chrome的inspect选项时,我可以在div中找到我需要的“membersTable”表 如何使用BeautifulSoup访问我可以在inspect中访问的membersTable。试试这个 import requests from bs4 import BeautifulSo

我正在使用Python和BeautifulSoup进行web抓取。我的目的是从中提取成员数据。大约有1685条记录

当我在Chrome上查看页面源代码时,我找不到表。似乎它动态地提取数据。但是当我使用Chrome的inspect选项时,我可以在div中找到我需要的“membersTable”表

如何使用BeautifulSoup访问我可以在inspect中访问的membersTable。

试试这个

   import requests
   from bs4 import BeautifulSoup


    url = "https://thehia.org/directory?&tab=1"
    response = requests.get(url)
    html = response.content

    soup = BeautifulSoup(html)
    table = soup.find('table', attrs={'class': 'membersTable'})

    row_list = []
    for row in table.findAll('tr',{'class':['normal']}):
        data= []
        for cell in row.findAll('td'):
            data.append(cell.text)
        row_list.append(data)

    print(row_list)

您可以模拟页面对内容发出的POST请求,然后使用hjson处理从响应中拉出的字符串中的无引号键

import requests, hjson
import pandas as pd

data = {'formId': '3721260'}
r = requests.post('https://thehia.org/Sys/MemberDirectory/LoadMembers', data=data)
data = hjson.loads(r.text.replace('while(1); ',''))
total = data['TotalCount']
structure = data['JsonStructure']
members = hjson.loads(structure)
df = pd.DataFrame([[member[k][0]['v'] for k in member.keys()] for member in members['members'][0]]
            ,columns = ['Organisation', 'City', 'State','Country'])
print(df)

感谢您的代码,但这不起作用,因为该表不存在于html中。如上图所示,我可以通过向下滚动来手动检查membersTable,但我不知道如何使用Python来实现这一点。