【问题标题】:Python Requests-HTML() unable to load cssPython Requests-HTML() 无法加载 css
【发布时间】:2020-05-18 04:10:32
【问题描述】:
我想下载一个页面的 css 并实现与浏览器中相同的外观。问题是抓取的结果看起来不同。
比如我要下载google的登陆页面。
这是我使用的代码:
import requests
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://www.google.com')
r.html.render()
file = open("start.html", "w")
file.write(r.text)
file.close()
【问题讨论】:
标签:
python
css
python-requests
python-requests-html
【解决方案1】:
css 通常在标签中。
尝试在bs4中解析页面
from bs4 import BeautifulSoup as BS
from requests_html import HTMLSession
session = HTMLSession()
r = session.get('https://www.google.com')
r.html.render()
soup=BS(r.text)
css = [link.get("href") for link in soup.findAll("link") if "stylesheet" in link.get("rel")]
现在 css 应该是所用 css 的链接列表