Python/美汤find_all()没有找到全部

标签 python python-3.x web-scraping beautifulsoup

当我使用 find_all() 时,我应该得到 100 个结果,但我只得到 25 个。

代码

这是我抓取的代码 tweakers并尝试返回类等于largethumb的每个元素。

完成后,我会过滤掉名称和价格。

my_url = 'https://tweakers.net/categorie/545/geheugen-intern/producten/'
uReq(my_url)
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")

#should be 100 tr object's
products = page_soup.find_all("tr", attrs={"class": "largethumb"})
for product in products:
    title = product.p.text
    price_container = product.find_all("p", {"class": "price"})
    price = price_container[0].text
    lijst = title, price
    print(lijst)

结果

结果是这个的 25 倍。

('Corsair Vengeance LPX CMK16GX4M2B3000C15', '€ 174,90')

最佳答案

相关网站默认显示 25 条搜索结果。如果您的网络浏览器有所不同,那是因为您的浏览器具有来自相关网站的 cookie。如果您想获得 100 个结果,请像这样编辑 my_url:

my_url = 'https://tweakers.net/categorie/545/geheugen-intern/producten/?pageSize=100&page=1'
uReq(my_url)
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")

# WILL be 100 tr object's
products = page_soup.find_all("tr", attrs={"class": "largethumb"})
for product in products:
    title = product.p.text
    price_container = product.find_all("p", {"class": "price"})
    price = price_container[0].text
    lijst = title, price
    print(lijst)

证明它有效:

>>> from requests import get
>>> from bs4 import BeautifulSoup
>>> my_url = 'https://tweakers.net/categorie/545/geheugen-
intern/producten/?pageSize=100&page=1'
>>> r = get(my_url)
>>> soup = BeautifulSoup(r.content, 'html5lib')
>>> len(soup.find_all('tr', attrs={"class": "largethumb"}))
100

如果您将鼠标悬停在左下角的 100 个结果按钮上,您会看到他们将您重定向到此网址。祝您抓取愉快!

关于Python/美汤find_all()没有找到全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47137551/

相关文章:

python - scrapy中在哪里定义项目自定义项目加载器?

python - Django 。 TemplateDoesNotExist 在自定义小部件的情况下

python - 将数据框中的列值拆分为空列值

python - 在 python 的 raw_input 中使用 %r

python - 有没有办法使用 python 知道 chrome 进程来自 chromedriver?

python - 使用 beautifulsoup 抓取动态网站

python - 如何向字典添加新键?

Python 3 - 解码包含十六进制和 unicode 混合的字节

python-3.x - 如何修复错误 pip install twisted 在 mac 上?

python - 将抓取 URL 从一个蜘蛛传递到另一个蜘蛛