tch*_*ore 0 python csv beautifulsoup
我正在尝试将一个URL列表放入一个csv文件中,我正在使用urllib2和BeautifulSoup从网页上抓取这些文件.我尝试将链接写入csv文件作为unicode,并转换为utf-8.在这两种情况下,每个字母都插入到一个新字段中.
这是我的代码(我至少尝试过这两种方式):
f = open('filename','wb')
w = csv.writer(f,delimiter=',')
for link in links:
w.writerow(link['href'])
Run Code Online (Sandbox Code Playgroud)
和:
f = open('filename','wb')
w = csv.writer(f,delimiter=',')
for link in links:
w.writerow(link['href'].encode('utf-8'))
Run Code Online (Sandbox Code Playgroud)
links 是一个如下所示的列表:
[<a href="#Flyout1" accesskey="2" class="quicklinks" tabindex="1" title="Skip to content">Quick Links: Skip to main page content</a>, <a href="#search" class="quicklinks" tabindex="1" title="Skip to search">Skip to Search</a>, <a href="#News" class="quicklinks" tabindex="1" title="Skip to Section table of contents">Skip to Section Content Menu</a>, <a href="#footer" class="quicklinks" tabindex="1" title="Skip to site options">Skip to Common Links</a>, <a href="http://www.hhs.gov"><img src="/ucm/groups/fdagov-public/@system/documents/system/img_fdagov_hhs_gov.png" alt="www.hhs.gov link" style="width:112px; height:18px;" border="0" /></a>]
Run Code Online (Sandbox Code Playgroud)
并非所有链接都有'href'密钥,但我在此处未显示的代码中检查了该密钥.在这两种情况下,正确的字符串都会写入csv文件,但每个字母都在新字段中.
有什么想法吗?
| 归档时间: |
|
| 查看次数: |
2237 次 |
| 最近记录: |