csv python文件没有保存在单独的行中

时间:2016-09-15 13:35:59

标签: python csv url beautifulsoup

  1. 代码正在运行,但保存的网址只保存在一行而不是另一行。

    from bs4 import BeautifulSoup
    import urllib2
    import requests
    import csv
    import re
    
    page=requests.get("https://www.tutorialspoint.com/python/dictionary_values.htm")
    
    data=BeautifulSoup(page.content)
    
    csv1=open("123.csv","wb+")
    
    for link in data.find_all('a'):
        print(link.get('href'))
        csv1.write(link.get('href'))
    
    
    
    csv1.close()
    
  2. 提前致谢

1 个答案:

答案 0 :(得分:0)

使用csv1.write模块

而不仅仅是csv.writer
from bs4 import BeautifulSoup
import urllib2
import requests
import csv
import re

page=requests.get("https://www.tutorialspoint.com/python/dictionary_values.htm")

data=BeautifulSoup(page.content)

csv1=open("123.csv","wb+")
csv1writer = csv.writer(csv1)

for link in data.find_all('a'):
    print(link.get('href'))
    csv1writer.writerow([link.get('href')])

csv1.close()