Python 无法使用Beautifulsoup将CSS爬网到HTML

Python 无法使用Beautifulsoup将CSS爬网到HTML,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,大家好,我正在尝试抓取正确的CSS,以便与从beautifulsoup创建的html表相匹配。表格已完成,但CSS未完成。有人能看一下我的代码,也许能提出一种更好的方式来抓取样式表吗 我可以看到两个问题: 1.我没有在与表匹配的页面上找到正确的样式表 2.如果没有任何问题的话,我在html文件中实现CSS是很麻烦的 import requests from bs4 import BeautifulSoup import pandas as pd import os import tabulate

大家好,我正在尝试抓取正确的CSS,以便与从beautifulsoup创建的html表相匹配。表格已完成,但CSS未完成。有人能看一下我的代码,也许能提出一种更好的方式来抓取样式表吗

我可以看到两个问题: 1.我没有在与表匹配的页面上找到正确的样式表 2.如果没有任何问题的话,我在html文件中实现CSS是很麻烦的

import requests
from bs4 import BeautifulSoup
import pandas as pd
import os
import tabulate
import urllib.request
import io
from bs4 import Comment


url = "https://www.etax.nat.gov.tw/etw-main/web/ETW183W2_10805/"

url_css = "https://www.etax.nat.gov.tw/etwmain/resources/web/css/main.fia.css"

soup = BeautifulSoup(urllib.request.urlopen(url).read(), features="html.parser",from_encoding='utf-16')

soup_table = soup.findAll('table')[0]

soup_css = BeautifulSoup(urllib.request.urlopen(url_css).read(), features="html.parser",from_encoding='utf-16')

with io.open("soup_table.html", "w", encoding='utf-16') as f:
   f.write(str(soup_table))
   f.write("<script>")
   f.write(str(soup_css))
   f.write("</script>")
导入请求
从bs4导入BeautifulSoup
作为pd进口熊猫
导入操作系统
进口表格
导入urllib.request
输入io
从bs4导入注释
url=”https://www.etax.nat.gov.tw/etw-main/web/ETW183W2_10805/"
url_css=”https://www.etax.nat.gov.tw/etwmain/resources/web/css/main.fia.css"
soup=BeautifulSoup(urllib.request.urlopen(url.read(),features=“html.parser”,来自\u encoding='utf-16')
soup\u table=soup.findAll('table')[0]
soup\u css=beautifulsou(urllib.request.urlopen(url\u css.read(),features=“html.parser”,来自\u encoding='utf-16')
将io.open(“soup_table.html”,“w”,编码为='utf-16')作为f:
f、 写入(str(汤表))
f、 写(“”)
f、 书写(str(soup_css))
f、 写(“”)

没有错误消息,只是没有正确的样式表看起来不正确。

css不应该在
中而不是
中吗?BeautifulSoup解析html。如果css不是内联的,您就不能用同样的方式解析它。查看此项以了解可能的解决方案,请阅读“它必须看起来如何”一节中的内容。