Python 通过Beautifulsoup将URL垂直写入csv文件

Python 通过Beautifulsoup将URL垂直写入csv文件,python,csv,beautifulsoup,Python,Csv,Beautifulsoup,我有一个大学课程的项目,要求我从美国人口普查局网站的一个页面中提取所有URL,并将其存储在CSV文件中。在大多数情况下,我已经知道了如何做到这一点,但由于某种原因,当数据附加到CSV文件时,所有条目都被水平插入。我希望数据是垂直排列的,这意味着第1行有列表中的第一项,第2行有第二项,依此类推。我尝试过几种方法,但数据总是以水平表示结束。我是python新手,显然对python语言没有足够的把握来解决这个问题。任何帮助都将不胜感激 我正在使用Beautifulsoup4和请求库解析网站。从网站中提

我有一个大学课程的项目,要求我从美国人口普查局网站的一个页面中提取所有URL,并将其存储在CSV文件中。在大多数情况下,我已经知道了如何做到这一点,但由于某种原因,当数据附加到CSV文件时,所有条目都被水平插入。我希望数据是垂直排列的,这意味着第1行有列表中的第一项,第2行有第二项,依此类推。我尝试过几种方法,但数据总是以水平表示结束。我是python新手,显然对python语言没有足够的把握来解决这个问题。任何帮助都将不胜感激

我正在使用Beautifulsoup4和请求库解析网站。从网站中提取所有的“a”标签非常简单,将这些“a”标签中的URL放入列表也非常清楚。但是,当我使用writerow函数将列表附加到CSV文件时,所有数据都会显示在一行中,而不是每个URL单独显示一行

import requests
import csv
requests.get
from bs4 import BeautifulSoup
from pprint import pprint

page = requests.get('https://www.census.gov/programs-surveys/popest.html')

soup = BeautifulSoup(page.text, 'html.parser')

## Create Link to append web data to
links = []

# Pull text from all instances of <a> tag within BodyText div
AllLinks = soup.find_all('a')

for link in AllLinks:
    links.append(link.get('href'))

with open("htmlTable.csv", "w") as f:
    writer = csv.writer(f)
    writer.writerow(links)

pprint(links)
导入请求
导入csv
请求。获取
从bs4导入BeautifulSoup
从pprint导入pprint
page=requests.get('https://www.census.gov/programs-surveys/popest.html')
soup=BeautifulSoup(page.text,'html.parser')
##创建将web数据附加到的链接
链接=[]
#从BodyText div内标记的所有实例中提取文本
AllLinks=soup.find_all('a'))
对于所有链接中的链接:
links.append(link.get('href'))
以open(“htmlTable.csv”,“w”)作为f:
writer=csv.writer(f)
writer.writerow(链接)
pprint(链接)

尝试通过在列表中添加url来创建列表

links.append([link.get('href')])
然后csv编写器会将每个列表与writerows放在一个新行上

writer.writerows(links)

尝试通过在列表中添加url来创建列表

links.append([link.get('href')])
然后csv编写器会将每个列表与writerows放在一个新行上

writer.writerows(links)
试试这个:

import requests
import csv
from bs4 import BeautifulSoup
page = requests.get('https://www.census.gov/programs-surveys/popest.html')

soup = BeautifulSoup(page.text, 'html.parser')
## Create Link to append web data to
links = []

# Pull text from all instances of <a> tag within BodyText div
AllLinks = soup.find_all('a')

for link in AllLinks:
    links.append(link.get('href'))

with open("htmlTable.csv", "w") as f:
    writer = csv.writer(f)
    for link in links:
        if (isinstance(link, str)):
            f.write(link + "\n",)
导入请求
导入csv
从bs4导入BeautifulSoup
page=requests.get('https://www.census.gov/programs-surveys/popest.html')
soup=BeautifulSoup(page.text,'html.parser')
##创建将web数据附加到的链接
链接=[]
#从BodyText div内标记的所有实例中提取文本
AllLinks=soup.find_all('a'))
对于所有链接中的链接:
links.append(link.get('href'))
以open(“htmlTable.csv”,“w”)作为f:
writer=csv.writer(f)
对于链接中的链接:
如果(isinstance(link,str)):
f、 写入(链接+“\n”,)
我更改它以检查给定链接是否确实是字符串,如果是,则在其后面添加一个换行符

试试这个:

import requests
import csv
from bs4 import BeautifulSoup
page = requests.get('https://www.census.gov/programs-surveys/popest.html')

soup = BeautifulSoup(page.text, 'html.parser')
## Create Link to append web data to
links = []

# Pull text from all instances of <a> tag within BodyText div
AllLinks = soup.find_all('a')

for link in AllLinks:
    links.append(link.get('href'))

with open("htmlTable.csv", "w") as f:
    writer = csv.writer(f)
    for link in links:
        if (isinstance(link, str)):
            f.write(link + "\n",)
导入请求
导入csv
从bs4导入BeautifulSoup
page=requests.get('https://www.census.gov/programs-surveys/popest.html')
soup=BeautifulSoup(page.text,'html.parser')
##创建将web数据附加到的链接
链接=[]
#从BodyText div内标记的所有实例中提取文本
AllLinks=soup.find_all('a'))
对于所有链接中的链接:
links.append(link.get('href'))
以open(“htmlTable.csv”,“w”)作为f:
writer=csv.writer(f)
对于链接中的链接:
如果(isinstance(link,str)):
f、 写入(链接+“\n”,)

我更改它以检查给定链接是否确实是字符串,如果是,则在其后面添加一个换行符

请添加您的导入使其成为一个完整的示例请添加您的导入使其成为一个完整的示例谢谢Matthew!谢谢Matthew做到了!