作者drunkofwind ()
看板Python
标题[问题] 爬虫到的连结 写入CSV後 每个字元被分开
时间Mon Jan 29 14:26:29 2018
大家好,
第一次尝试写爬虫
目前成功从资料库抓取之後要真正爬虫的连结,
------------------------------------------------------------------------
urls = ['
http://www.fishbase.org/Topic/List.php?group=9&start=0',
'
http://www.fishbase.org/Topic/List.php?group=9&start=500',
'
http://www.fishbase.org/Topic/List.php?group=9&start=1000']
f = open("link.csv","w", newline='')
data = []
for url in urls:
web_data = requests.get(url)
soup = BeautifulSoup(web_data.text, 'html.parser')
splink = soup.find('table')
for alink in splink.find_all('a'):
data = [alink.get('href')]
w = csv.writer(f, delimiter="\t")
w.writerows(data)
f.close()
--------------------------------------------------------------------------
但是要把连结write成csv後,连结网址每个字被分开在不同col,
https://imgur.com/a/5x8Rf
後来google到,
在csv.writer()里加入 delimiter="\t",写到CSV 连结的字元就不会分开,
但是把连结复制贴到记事本,其实字元还是被分开
https://imgur.com/a/iTFnJ
所以把汇入同一个CSV档在用python打开,连结就会变成
https://imgur.com/a/2Nl1A
请问该怎样才能把让每个连结是一个字串? 谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.112.70.50
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1517207197.A.A86.html
※ 编辑: drunkofwind (140.112.70.50), 01/29/2018 14:35:29
1F:推 ckc1ark: w.writerow(data) 就好? 01/29 14:42
WOW~~我把w.writerows(data) 改成 w.writerow(data),
问题就解决了@@ 太神奇了~~~大感谢~~~
※ 编辑: drunkofwind (140.112.70.50), 01/29/2018 14:47:28