如何使用beautifulsoup從腳本標籤內部抓取數據?

灰燼

我試圖從網站的腳本中提取一些數據。我能夠從整個頁面獲取數據,但是我被困在如何從腳本標籤內部獲取數據。這是我如何得到湯。

import requests
from bs4 import BeautifulSoup

searchf = input("Enter the product you want: ")

url =f"https://www.daraz.com.np/catalog/?q={searchf}&_keyori=ss&from=input&spm=a2a0e.searchlist.search.go.12ec45adZnn1MP"

r = requests.get(url)
contents = r.content

soup = BeautifulSoup(contents,'html.parser')
print(soup.prettify())

這是我想要抓取的網站,我只想獲取那裡列出的產品的名稱。https://www.daraz.com.np/catalog/?q=cars&_keyori=ss&from=input&spm=a2a0e.searchlist.search.go.3b834ce4JPBUHm

法茲魯

數據也由 javacript 動態加載。轉到網絡選項卡 > xhr/fetch> 標頭,然後您將看到 api url,如果您單擊預覽選項卡,您將看到數據,總項目數為 4080,每個頁麵包含 40 個項目,我使用查詢字符串參數作為與 api url 一起發送的參數。

代碼:

import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'}

api_url = 'https://www.daraz.com.np/catalog/'

params = {
    'keyori': 'ss',
    'ajax': 'true',
    'from': 'input',
    #'page': '2',
    'q': 'cars',
    'spm': 'a2a0e.searchlist.search.go.3b834ce4JPBUHm'
    
    }

for num in range(0, 4080, 40):
    params['totalResults'] = num
    print(num)
    resp = requests.get(api_url, params=params, headers=headers).json()

    items = resp['mods']['listItems']
    for item in items:
        name = item['name']
        print(name)

輸出:

Hot wheels 3 Pack Cars
XimiVogue  Pull-Back-And-Go Alloy Suv Car Toy With Sound (Bmw Mini)
Kids Spider Stunt Flip Car
Funny Diy Electric Thomas Train Truck Set Toy Set Orange
Monster Trucks Inertia Car Toys For Kids
Remote Control Transformer Car
Remote Control Emulation Car
Spiderman  Friction Stunt Car
Remote Control Car
360 Degree Spinning Rolling stunt Farm Truck Super Tipping Car for Kids
Monster Trucks Friction Powered Inertia Cars Toys For Kids (Color May Vary)
Gallant Toy Bus-Set Of 2
Remote Control Transformer Car
19026B Express Train Set - Battery Operated Black Train Toys for Kids Aged 3+ Years
Friction Powered Monster Truck Toy (Multicolor) Pack Of 1
Toy Train
Model Concept R/C Car
Friction Powered 3-Pack Mini Push and Go Car Truck Jam Playset
Makes Your Kids Happy -  Remote Control Vehicle Car For Kids
2.4Ghz 1/18 Rc Rock Crawler Vehicle Buggy Car 4 Wd Shaft Drive High Speed Remote Control 4X4 Monster Off Road Truck
XimiVogue  Pull-Back-And-Go Alloy Suv Car Toy With Sound
Kids Spider Stunt Flip Car Small Size
Black ForMula Remote Control Car For Kids

... 以此類推(總項目 4080)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 Bluehost 共享託管計劃中,如何從 PHP 腳本創建數據庫?

Python BOTO3 腳本未在標籤內返回名稱

通過使用beautifulsoup迭代從選定數字中刪除標籤

使用beautifulsoup從導航標籤中提取數據

使用 beautifulsoup 從網站上抓取數據 - 嵌套

使用 BeautifulSoup - Python 從 www.transfermarkt.co.uk 抓取數據

如何將從 image_dataset_from_directory 獲得的數據集拆分為數據和標籤?

使用 Javascript Puppeteer 獲取標籤的內部文本

如何使用應用程序腳本從其他工作表匹配和導入數據?

如何從 jQuery 中的外部文件獲取 JSON 數據並確保在腳本的其餘部分之前加載數據

如何使用beautifulsoup從多個頁面抓取數據

從內部 <a href> 鏈接顯示標籤

如何使用硒從網站上抓取數據

Python / BeautifulSoup - 從 Clinicaltrials.gov API 抓取 XML 數據 - 解析 XML 父/子標籤中的數據

如何在 Python 中使用 Selenium 從 th 和 td 標籤中提取數據?

如何立即從實例本身的用戶數據腳本終止 EC2 實例?

如何從字符串加載腳本標籤?

如何通過ajax腳本將數據從html傳遞回python文件,以便在同一頁面上呈現更多數據

使用 BeautifulSoup 從 Coinmarketcap 中抓取歷史比特幣數據

如何只打印標籤內的數字

如何從 JSON 內部嵌套數據

如何從表中抓取數據

如何使用其他數據模型類型腳本處理枚舉數據模型?

使用請求和 BeautifulSoup 從網站的不同選項卡中抓取數據

如何從標籤內的屬性獲取值

嘗試使用PS腳本從SQL中提取數據

如何使用 axis() 為包含 NA 的數據添加自定義軸標籤

腳本標籤中的腳本標籤?

我應該如何刮掉 <ul> 中的所有 <em> 標籤內部文本並將它們變成熊貓數據框?