美文网首页Python爬虫作业
交作业 爬美股吧

交作业 爬美股吧

作者: Snow__ | 来源:发表于2017-05-29 14:59 被阅读0次

作业要求:
东方财富网美股吧贴子数据 包含:浏览数、评论数 、帖子标题 、帖子内容 、回复人、 回复时间、 回复内容http://guba.eastmoney.com/list,meigu.html

这个网站作为练习提升挺大的,有很多小细节要抠,花了好多时间,还是没搞完。目前能爬,但是有一些编码和最后数据处理的问题还没解决。不知为何。先交。

# -*- coding:utf-8 -*-
import requests
from lxml import etree
import csv

import sys

reload(sys)
sys.setdefaultencoding('utf-8')

start_url = "http://guba.eastmoney.com/list,meigu_1.html"
headers = {
    "User-Agent": "Mozilla / 5.0(Windows NT 6.1;Win64;x64)"
                  "AppleWebKit / 537.36(KHTML, likeGecko)"
                  "Chrome / 58.0.3029.110"
                  "Safari / 537.36"
}


# def get_total_page(start_url):
#    html = requests.get(url=start_url, headers=headers).content
#    selector = etree.HTML(html)
#    sum_page = selector.xpath("//span[@class='sumpage']/text()")
#    return sum_page


def parse_title():
    # sum_page = get_total_page(start_url)
    rows = []
    for num in range(1, 23):
        url = "http://guba.eastmoney.com/list,meigu_" + str(num) + ".html"
        html = requests.get(url=url, headers=headers).content
        selector = etree.HTML(html)
        items = selector.xpath("//div[@id='articlelistnew']/div[position()>1 and position()<last()]")
        for item in items:
            title = item.xpath("span[@class='l3']/a/text()")[0].decode(encoding='utf-8')
            author = item.xpath("span[@class='l4']/a/text()")
            read = item.xpath("span[@class='l1']/text()")[0]
            comment_num = item.xpath("span[@class='l2']/text()")[0]
            post_time = item.xpath("span[@class='l6']/text()")[0]
            last_update = item.xpath("span[@class='l5']/text()")[0]
            link = item.xpath("span[@class='l3']/a/@href")
            rows.append(
                {'title': title, 'author': author, 'read': read, 'comment_num': comment_num, 'post_time': post_time,
                 'last_update': last_update, 'link': link})
    return rows


def parse_content_comment():
    links = []
    temp = parse_title()
    for item in temp:
        links.append(item['link'][0])
    rows = []
    for link in links[0:8]:
        url = "http://guba.eastmoney.com/" + link
        html = requests.get(url=url, headers=headers).content
        selector = etree.HTML(html)
        lines = {}
        lines['content'] = selector.xpath("//div[@class='stockcodec']/text()")
        comments = selector.xpath("//div[@id='zwlist']")
        for item in comments:
            if item.xpath("div[@class='zwli clearfix']"):
                name = item.xpath("div/div/div/div[@class='zwlianame']/span/a/text()")
                comment = item.xpath("div/div/div/div[@class='zwlitext stockcodec']/text()")
                time = item.xpath("div/div/div/div[@class='zwlitime']/text()")
                lines['name'] = name
                lines['comment'] = comment
                lines['time'] = time
            else:
                lines['name'] = 'none'
                lines['comment'] = 'none'
                lines['time'] = 'none'
            rows.append(lines)
    for link in links[8:]:
        url = "http://guba.eastmoney.com" + link
        html = requests.get(url=url, headers=headers).content
        selector = etree.HTML(html)
        lines = {}
        lines['content'] = selector.xpath("//div[@class='stockcodec']/text()")
        comments = selector.xpath("//div[@id='zwlist']")
        for item in comments:
            if item.xpath("div[@class='zwli clearfix']"):
                name = item.xpath("div/div/div/div[@class='zwlianame']/span/a/text()")
                comment = item.xpath("div/div/div/div[@class='zwlitext stockcodec']/text()")
                time = item.xpath("div/div/div/div[@class='zwlitime']/text()")
                lines['name'] = name
                lines['comment'] = comment
                lines['time'] = time
            else:
                lines['name'] = 'none'
                lines['comment'] = 'none'
                lines['time'] = 'none'
            rows.append(lines)
    return rows


if __name__ == "__main__":
    headlines1 = ['title', 'author', 'read', 'comment_num', 'post_time', 'last_update', 'link']
    headlines2 = ['content', 'name', 'comment', 'time']
    #    get_total_page(start_url)
    rows1 = parse_title()
    rows2 = parse_content_comment()
    with open('eastmoney1.csv', 'w') as f:
        f_csv = csv.DictWriter(f, headlines1)
        f_csv.writeheader()
        f_csv.writerows(rows1)
    with open('eastmoney2.csv', 'w') as f:
        f_csv = csv.DictWriter(f, headlines2)
        f_csv.writeheader()
        f_csv.writerows(rows2)

思路很简单,代码也很好懂。后来突然发现评论还有分页的。先放着,把这里处理完先。
结果

image.png image.png

有点丑陋。。。

相关文章

  • 交作业 爬美股吧

    作业要求:东方财富网美股吧贴子数据 包含:浏览数、评论数 、帖子标题 、帖子内容 、回复人、 回复时间、 回复内容...

  • 爬美股吧修改1

    第一部分修改后已经能够正常显示,主要问题是不同的帖子有些地方缺省需要补齐,不然不好处理。处理文本的能力还是要加强啊...

  • 爬美股吧 最终修改

    先上代码 之前不知道为何会乱码,这个星期这个作业就一直在我心里纠缠着我,今天冷静的分析了一下原因,终于发现了问题,...

  • Python作业20170526:美股吧爬虫

    作业链接 帖子网页分析 帖子导航 http://guba.eastmoney.com/list,meigu_2.h...

  • 交篇作业吧

    人啊,很容易颓废,需要一根线像拉住风筝一样拉着自己,才能按时去完成任务。突然觉得,这每周一篇的模式可以约束一个...

  • 港美股软件开发开户需求浅析以及开户步骤

    港美股软件开发开户需求浅析以及开户步骤: 国内港美股开户需求较大:目前数据全球化、交...

  • 交作业 爬简书首页

    好久没写爬虫了,有些生疏了,虽然之前也只是会简单的爬,先记录一下。用的是Scrapy框架。首先 scrapy s...

  • 美好的春天

    周末的作文交作业吧~

  • 普通人定投到底适用于哪个领域?

    在A股,美股,港股等众多交易市场中有很多可以用作定投的标的,比如A股的贵州茅台,美股的苹果,港股的腾讯,都是股市交...

  • 普通人定投这事基本只适用于币圈

    在A股,美股,港股等众多交易市场中有很多可以用作定投的标的,比如A股的贵州茅台,美股的苹果,港股的腾讯,都是股市交...

网友评论

    本文标题:交作业 爬美股吧

    本文链接:https://www.haomeiwen.com/subject/ktnkfxtx.html