如何使用beautifulsoup解析<pre>标记中的数据?

2024-06-24 13:43:05 发布

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

当我试图从以下网站上获取数据时

url=https://bedbathandbeyond.ugc.bazaarvoice.com/2009-en_us/1061083288/reviews.djs?format=embeddedhtml&page=4&scrollToTop=true

我是从bedbathbeyond网站上得到的,如果我使用request和beautifulsoup,我就什么也得不到。为什么?在

代码:

r = requests.get(url)
soup = BeautifulSoup(r.content,'lxml')
soup.find_all('span', class_ = 'BVRRReviewAbbreviatedText')

返回值为空:[]


Tags: httpscomformaturl网站pageenus
2条回答

我使用了^{},因为materials对象包含多个键(BVRRRatingSummarySourceIDBVRRSecondaryRatingSummarySourceID和{}),如果您需要的话,用regex从它的值中获取HTML要困难得多。在

from bs4 import BeautifulSoup
import js2py
import requests

r = requests.get('https://bedbathandbeyond.ugc.bazaarvoice.com/2009-en_us/1061083288/reviews.djs?format=embeddedhtml')

pattern = (r'var'
           r'\s+'
           r'materials'
           r'\s*=\s*'
           r'{"BVRRRatingSummarySourceID".*}')

js_materials = re.search(pattern, r.text).group()
obj = js2py.eval_js(js_materials).to_dict()
html = obj['BVRRSourceID']
soup = BeautifulSoup(html, 'lxml')
spans = soup.select('span.BVRRReviewAbbreviatedText')
^{pr2}$

在下面的示例中,我只使用了BVRRSourceID键下的HTML,但是您可以通过将值连接在一起来使用整个HTML:

html = ''.join(obj.values())

如果您想使用lxml解析器,不要忘记安装js2pypip install js2py和{}。在

您可以使用selenium webdriver来获取您感兴趣的html内容。例如

from selenium import webdriver


def get_html(url):
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get(url)

    time.sleep(5)
    html_content = driver.page_source.strip()
    return html_content

相关问题 更多 >