使用启用 Cookie 的 Python 请求进行网络抓取

Web Crawling Using Python Request with Cookies Enabled

我正在尝试抓取此特定网页: https://www.grainger.com/category/abrasives/abrasive-blasting/abrasive-blasting-cabinets

使用以下代码行:

page = requests.get("https://www.grainger.com/category/abrasives/abrasive-blasting/abrasive-blasting-cabinets")

但是,该页面带有默认 "table" 视图。但是我要抓取的网页是启用"list view is enabled. How can I request the web page with "list”视图时如下图所示:

通过 Google Chrome 的网络选项卡查看,在我看来,该网站通过设置 cookie 知道显示哪个视图,因此如果需要,您可以在请求中设置此 cookie以列表形式查看。

requests.get("https://www.grainger.com/category/abrasives/abrasive-blasting/abrasive-blasting-cabinets", cookies={'pv':'list'})

以后您可以检查 this reference 以帮助您了解浏览时实际发生的情况。