【发布时间】:2020-01-24 01:16:39
【问题描述】:
我使用 BeautifulSoup 抓取网站以保存到 csv。当我打开 csv 时,只有标题、标题,没有数据(我抓取的链接)。
我已经尝试过“lxml”,所以我切换到了 html.parser。
from bs4 import BeautifulSoup
import requests
import csv
page = requests.get('https://www.census.gov/programs-surveys/popest.html')
raw_html = page.text # declare the raw_html var
soup = BeautifulSoup(raw_html, 'html.parser') # parse the html
T = [["US Census Bureau Links"]] #Title
I = page.text
for link in soup.find_all('a', href=True):
print(link['href'])
with open("US_Census_Bureau_links.csv","w",newline="") as f:
cw=csv.writer(f)
cw.writerows(T)
cw.writerows(I)
f.close()
当我运行它时,我得到了 8 页的完整链接。但输出 csv 中没有链接。
【问题讨论】:
标签: html csv parsing beautifulsoup