使用Python Beautiful Soup对Javascript表(带有网格和列表视图)进行Web抓取

2024-10-01 13:43:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图解析来自这个网站的json表中的数据

url-https://boxes.mysubscriptionaddiction.com/subscription_boxes_for/food

我主要需要列出的所有食品订购箱的名称、等级和说明。我在这里面临一些挑战。一个是表有两个视图-网格视图和列表视图。如何指定代码中引用的表视图?第二个是我得到了一份工作

ValueError - Timeout value connect was Timeout(connect=<object object at 0x000002767CECD5C0>, 
read=<object object at 0x000002767CECD5C0>, total=None), but it must be an int, float or None.

不确定这意味着什么。
我的代码:

from pandas.io.html import read_html
from selenium import webdriver
import json
import requests
import os
import sys
from bs4 import BeautifulSoup
import requests


driver = webdriver.Firefox(executable_path='C:\Drivers\geckodriver.exe')

driver.get('https://boxes.mysubscriptionaddiction.com/subscription_boxes_for/food')


table = driver.find_element_by_xpath('/html/body/div[3]/div/span/div[2]/div/div[1]/div[3]/div[3]/table')

table_html = table.get_attribute('innerHTML')

bs = BeautifulSoup(table_html, 'html.parser')

rows = bs.select('tbody tr')

print(bs)

Tags: fromhttpsimportdivcom视图jsonbs
1条回答
网友
1楼 · 发布于 2024-10-01 13:43:54

下面是如何获取您要查找的数据:(data是包含信息的dict)

import requests
from bs4 import BeautifulSoup
import json

scrape_url = 'https://boxes.mysubscriptionaddiction.com/subscription_boxes_for/food'

r1 = requests.get(scrape_url)
page = r1.content
soup = BeautifulSoup(page, 'html.parser')
scripts = soup.find_all('script')

data_str = scripts[11].contents[0].strip()
data = json.loads(data_str,strict=False)
print(data['itemListElement'])

相关问题 更多 >