首页 > 用Python成功抓取了url和文章标题,写入csv后打开只有一条数据,是哪里出错了呢?

用Python成功抓取了url和文章标题,写入csv后打开只有一条数据,是哪里出错了呢?

代码如下:

# -*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
import csv

user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'

def get_data(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text,'lxml')
    soup = soup.find('div',{'id':'listZone'}).findAll('a')
    return soup

csvfile = open('D:/Python34/test.csv','wt')
writer = csv.writer(csvfile)
try:
    for links in get_data('http://finance.qq.com/gdyw.htm'):
        csvrow1=[]
        csvrow1.append('http://finance.qq.com/'+links.get('href'))
    for title in get_data('http://finance.qq.com/gdyw.htm'):
        csvrow2=[]
        csvrow2.append(title.get_text())
    csvrow3=list(zip(csvrow1,csvrow2))
    writer.writerow(csvrow3)
finally:
    csvfile.close()

你的csvrow1和csvrow2放错位置了,你放在循环内部,每次循环都直接被初始化为空列表了

csvrow1=[]
csvrow2=[]
for links in get_data('http://finance.qq.com/gdyw.htm'):
        csvrow1.append('http://finance.qq.com/'+links.get('href'))
    for title in get_data('http://finance.qq.com/gdyw.htm'):
        csvrow2.append(title.get_text())
【热门文章】
【热门文章】