【发布时间】:2020-08-17 10:31:30
【问题描述】:
所以我想使用 Beautiful Soup 来抓取这个页面:https://www.nseindia.com/option-chain#optionchain_equity 并使用 requests 模块访问它。但我猜 requests 只保存基本的 html 而不是该页面中的主表。使用 chrome 下载“网页,完成”的作品,但我怎样才能在 python 中自动化呢?同样没有这些标头,请求会超时,所以我猜是有必要的。 代码:
import requests
url = "https://www.nseindia.com/option-chain#optionchain_equity"
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/80.0.3987.149 Safari/537.36',
'accept-language': 'en,gu;q=0.9,hi;q=0.8', 'accept-encoding': 'gzip, deflate, br'}
response = requests.get(url, headers=headers, timeout=5)
file = open("nse.html", "w")
file.write(response.text)
【问题讨论】:
-
基本 HTML 是什么意思?
-
完成后记得
file.close(),用with打开可能会更方便
标签: python python-3.x web-scraping python-requests