Web scraping 使用BeautifulSoup创建Web抓取Javascript表

Web scraping 使用BeautifulSoup创建Web抓取Javascript表,web-scraping,beautifulsoup,Web Scraping,Beautifulsoup,我对使用各种网站进行网页抓取和原型制作比较陌生。我在抓取似乎加载了Javascript的表时遇到了困难。任何帮助都将不胜感激。以下是我的代码: import requests from bs4 import BeautifulSoup url='https://onlineservice.cvo.org/webs/cvo/register/#/search/ toronto/0/1/0/10' response = requests.get(url) soup = BeautifulSou

我对使用各种网站进行网页抓取和原型制作比较陌生。我在抓取似乎加载了Javascript的表时遇到了困难。任何帮助都将不胜感激。以下是我的代码:

import requests
from bs4 import BeautifulSoup


url='https://onlineservice.cvo.org/webs/cvo/register/#/search/
toronto/0/1/0/10'

response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
tables = soup.find_all(class_='table')
print(tables)

试试下面的url,一眨眼就可以获得所有信息。您可以在“网络”选项卡下的xhr请求下使用chrome开发工具检索该url。试一试:

import requests

URL = 'https://onlineservice.cvo.org/rest/public/registrant/search/?query=%20toronto&status=0&type=1&skip=0&take=427'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
}

response = requests.get(URL,headers=headers,verify=False)

for items in response.json()['result']:
    lastname = items['lastName']
    firstname = items['firstName']
    commonname = items['commonName']
    status = items['registrationStatus']['name']
    print(lastname,firstname,commonname,status)
部分结果:

Aadoson Andres Andres Active
Aarabi Alireza Allen Active
Aarnes Turi Turi Expired
Abbasi Tashfeen Tashfeen Active
Abbott Jonathan Jonathan Resigned
Abd El Nour Emad Emad Active
Abdel Hady Medhat Hady Active
Abdelhalim Khaled Khaled Active

该表加载了js,因此您首先需要一个能够在该页面上运行js的客户端,然后解析结果……我相信selenium是推荐的工具。但您也可以查看本教程,看看它是否有帮助:感谢您的回复,pyQT还有其他替代方案吗?我在网络选项卡上,单击了xhr请求,但我找不到URL,无法了解它是如何工作的。