无法使用 python3 中的 bs4 解析包含“.html#/something”的地址

Cannot parse address which contain ".html#/something" using bs4 in python3

我的目标是解析第二页的图像。我为此使用 bf4 和 Python3 。 请查看这两页:

1) 只有 page 所有 4 种颜色的图像(我可以解析此页面);

2) 和 page 仅包含一种颜色的图像(本例中为铬色)。我需要解析这个页面。

使用浏览器我可以看到第二页与第一页不同。但是,使用 bs4 我在第一页和第二页上得到了相似的结果,因为 python 在第二页地址中没有识别出这个“.html#/kolor-chrom”。

首页地址:“https://azzardo.com.pl/lampy-techniczne/2111-bross-1-tuba-lampa-techniczna-azzardo.html”。

第二页地址:“https://azzardo.com.pl/lampy-techniczne/2111-bross-1-tuba-lampa-techniczna-azzardo.html#/kolor-chrom”.

重现代码:

from bs4 import BeautifulSoup
import requests

adres1 = "https://azzardo.com.pl/lampy-techniczne/2111-bross-1-tuba-lampa-techniczna-azzardo.html"
adres2 = "https://azzardo.com.pl/lampy-techniczne/2111-bross-1-tuba-lampa-techniczna-azzardo.html#/kolor-chrom"

def parse_one_page(adres):
    """Parse one page and get all the img src from adres"""
    # Use headers to prevent hide our script
    headers = {'User-Agent': 'Mozilla/5.0'}
    # Get page
    page = requests.get(adres, headers=headers)  # read_timeout=5
    # Get all of the html code
    soup = BeautifulSoup(page.content, 'html.parser')
    # Find div
    divclear = soup.find_all("div", class_="clearfix")
    divclear = divclear[9]
    # Find img tag
    imgtag = [i.find_all("img") for i in divclear][0]
    # Find src
    src = [i["src"] for i in imgtag]
    # See how much images are here
    print(len(src))
    # return list with img src
    return src


print(parse_one_page(adres1))
print(parse_one_page(adres2))

在 运行 这些代码之后,您会看到这两个地址的输出是相似的:两个地址都有 24 张图像。第一页中有 24 张图片(正确)。但是在第二页这里必须只有2张图片,而不是24张(不正确)!

希望有人能帮助我如何正确使用 bs4 解析 python3 中的第二页。

是的,看起来无法使用 bs4 解析此类响应式页面