python - Python Webscrape HTML to CSV File For Loop
问题描述
我对 Python 还是很陌生,我很难用我的 For 循环来提取某个站点上的所有 Web 链接。这是我的代码:
import requests
import csv
from bs4 import BeautifulSoup
j= [["Population and Housing Unit Estimates"]] # Title
k= [["Web Links"]] # Column Headings
example_listing='https://www.census.gov/programs-surveys/popest.html' #Source
r=requests.get(example_listing) #Grab page source html
html_page=r.text
soup=BeautifulSoup(html_page,'html.parser') #Build Beautiful Soup object to help parse the html
with open('HTMLList.csv','w',newline="") as f: #Choose what you want to grab
writer=csv.writer(f,delimiter=' ',lineterminator='\r')
writer.writerows(j)
writer.writerows(k)
for link in soup.find_all('a'):
f.append(link.get('href'))
if not f:
""
else:
writer.writerow(f)
f.close()
任何帮助深表感谢。我真的不知道从这里去哪里。谢谢!
解决方案
假设您尝试将站点中的 URL 保存到 CSV 文件中 - 每行一个 URL。首先不要重用f
,那是为了文件。您可以通过将链接包含在数组中来直接将链接写入 CSV writer.writerow([link.get('href')])
。希望对您有所帮助。否则,请编辑您的问题并添加更多详细信息。
import csv
import requests
from bs4 import BeautifulSoup
j= [["Population and Housing Unit Estimates"]] # Title
k= [["Web Links"]] # Column Headings
example_listing='https://www.census.gov/programs-surveys/popest.html' #Source
r=requests.get(example_listing) #Grab page source html
html_page=r.text
soup=BeautifulSoup(html_page,'html.parser') #Build Beautiful Soup object to help parse the html
with open('HTMLList.csv','w', newline="") as f: #Choose what you want to grab
writer=csv.writer(f, delimiter=' ',lineterminator='\r')
writer.writerows(j)
writer.writerows(k)
for link in soup.find_all('a'):
url = link.get('href')
if url:
writer.writerow([url])
推荐阅读
- keycloak - 需要一个名为 aud 的有效负载属性,其值为 (n) 字符串
- c++ - [basic.execution] p5 句子 2 的意图
- google-apps-script - 有没有办法根据用户下拉选择提交对不同电子表格的谷歌表单响应?
- api - 如何使用 GitHub GraphQL API 获取具有特定主题的所有存储库?
- java - 使布局适用于所有具有固定尺寸的 Android 设备
- javascript - 两种代码之间的区别 - 模块
- asp.net - ASP.NET 使用 iframe 进行 3ds - 如何返回到托管页面
- python - 更新 config.ini 时 IronPython 包“System.UnauthorizedAccessException”
- python - 自动确定绘图大小 matplotlib
- ruby-on-rails - 控制器“params”在测试环境和开发环境中表现不同