1 html python beautifulsoup web-scraping
我想用 beautifulsoup 使用下面的代码编写该网站上产品的价格,但是当我编写代码时,列表返回空。
将请求导入为 req from bs4 将 BeautifulSoup 导入为 bs
url =“https://www.migros.com.tr/temel-gida-c-2?sayfa=1”
headers = { 'User-Agent': ( 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) ' 'AppleWebKit/537.36 (KHTML,如 Gecko) Chrome/39.0.2171.95 Safari/537.36' ) }
urrunler = []
rx = req.get(url, headers=headers) sympo = bs(rx.text, 'html.parser') soup = bs(rx.content, 'lxml') print(sympo.findAll('span', {'类': '金额'}))
print(sympo.findAll('span', {'class': 'amount'}))
[]
Run Code Online (Sandbox Code Playgroud)
您看到的数据是通过 Javascript 加载的。您可以使用requests/ jsonmodule 来加载它:
import json\nimport requests\n\n\nurl = "https://www.migros.com.tr/rest/search/screens/temel-gida-c-2?sayfa=1"\ndata = requests.get(url).json()\n\n# uncomment this to print all data:\n# print(json.dumps(data, indent=4))\n\nfor p in data["data"]["searchInfo"]["storeProductInfos"]:\n print(\n "{:<35} {:<10} {:<10}".format(\n p["name"], p["regularPrice"], p["salePrice"]\n )\n )\nRun Code Online (Sandbox Code Playgroud)\n印刷:
\nMaydanoz Adet 445 445 \nSo\xc4\x9fan Kuru D\xc3\xb6kme Kg 195 195 \nMigros Havu\xc3\xa7 Beypazar\xc4\xb1 Paket Kg 875 750 \nDomates Kg 1495 1495 \nKabak Sak\xc4\xb1z Kg 1990 1990 \nDereotu Adet 930 930 \nRoka Demet 603 603 \nSalata K\xc4\xb1v\xc4\xb1rc\xc4\xb1k Adet 1090 1090 \nPatl\xc4\xb1can Kemer Kg 1990 1990 \nSo\xc4\x9fan Taze Demet 925 925 \nH\xc4\xb1yar Kg 1790 1790 \nDomates Salk\xc4\xb1m Kg 2290 2290 \nBiber K\xc4\xb1rm\xc4\xb1z\xc4\xb1 Kg 2190 2190 \nBrokoli Kg 3450 3450 \nAtom Salata Adet 1206 1206 \nKereviz Kg 875 875 \nKarnabahar Kg 1390 1390 \nIspanak Kg 1450 1450 \nPatates Taze Kg 556 556 \nBiber K\xc3\xb6y Usul\xc3\xbc Kg 2990 2990 \nNane Adet 631 631 \nBiber Sivri Kg 2690 2690 \nP\xc4\xb1rasa Kg 930 930 \nLahana Beyaz Kg 595 595 \nBiber Dolmal\xc4\xb1k Kg 2702 2702 \nDomates \xc5\x9eeker 250 G 837 837 \nLahana K\xc4\xb1rm\xc4\xb1z\xc4\xb1 Kg 1206 1206 \nPatates Ekonomik Boy File Kg 445 445 \nPancar Kg 743 743 \nDomates Pembe Kg 1850 1850 \nRun Code Online (Sandbox Code Playgroud)\n