楼主: 志地有深
877 0

[求助] 需要爬取微博页面关于医保关键词的内容,已经获得爬虫代码,但是不会替换一些内容 [推广有奖]

  • 1关注
  • 0粉丝

小学生

92%

还不是VIP/贵宾

-

威望
0
论坛币
0 个
通用积分
67.6055
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
83 点
帖子
4
精华
0
在线时间
12 小时
注册时间
2021-8-8
最后登录
2023-11-2

楼主
志地有深 发表于 2021-8-10 20:33:23 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
from urllib.parse import urlencode
import requests
from pyquery import PyQuery as pq
import os
import re
import xlwt
import pandas as pd

current_Path = os.path.dirname(os.path.abspath(__file__)) + '\\'

base_url = 'https://s.weibo.com/'

headers = {
    'Host': 's.weibo.com',
    'Refer': 'https://s.weibo.com/weibo?q=%E5%8C%BB%E4%BF%9D&typeall=1&suball=1&Refer=g',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 Edg/92.0.902.67'
}


# 搜索医保满意
def get_Research(research_Words, page):
    params = {
        'q': 医保满意,
        'Refer': 'SWeibo_box',
        'page': str(page)
    }
    url = 'https://s.weibo.com/weibo?q=%E5%8C%BB%E4%BF%9D%E6%BB%A1%E6%84%8F&typeall=1&suball=1&Refer=SWeibo_box' + urlencode(params)
    # print(url)
    # print(urlencode(params))

    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.text
    except requests.ConnectionError:
        return None


def get_Information(research_Words, page):
    res = []
    html = get_Research(research_Words, page)
    doc = pq(html)
    # print(doc)
    with open(current_Path + 'test.txt', 'w+', encoding='utf8') as f:
        f.write(html)
    # items = doc(".content").items()
    items = doc("div[class='card']").items()

    for li in items:
        temp_Info_Dict = {}

        ###抽取昵称
        info = li.find('div')('.name')
        nick_Name = info.attr('nick-name')
        temp_Info_Dict['博主id'] = nick_Name
        ###抽取内容
        # text = li('.txt')
        text = li("p[node-type='feed_list_content_full']>a")
        temp_Info_Dict['微博正文'] = text.text()
        if temp_Info_Dict['微博正文'] == '':
            text = li("p[node-type='feed_list_content']>a")
            temp_Info_Dict['微博正文'] = text.text()
        # print(text.text())
        # print(temp_Info_Dict['微博正文'])
        ###时间&设备
        time_Device = li("p[class='from']>a").text()
        temp_Info_Dict['发布时间'] = time_Device
        ###转发数 评论数 点赞数
        forwards = li('.card-act li').items()  # ("a[action-type='feed_list_forward']")
        for i, forward in enumerate(forwards):
            num = re.sub("\D", "", forward.text())
            # print(num)
            if num == '':
                num = 0
            else:
                num = int(num)
            if i == 1:
                temp_Info_Dict['转发'] = num
            elif i == 2:
                temp_Info_Dict['评论'] = num
            elif i == 3:
                temp_Info_Dict['点赞'] = num
            # print(forward.text())
        res.append(temp_Info_Dict)
        # print(res)
    return res
    ###发布时间


##导出excel
def export_excel(export):
    pf = pd.DataFrame(list(export))
    # 指定字段顺序
    order = ['博主id', '微博正文', '转发', '评论', '点赞', '发布时间']
    pf = pf[order]
    file_path = pd.ExcelWriter(current_Path + 'name.xlsx')
    pf.fillna(' ', inplace=True)
    # 输出
    pf.to_excel(file_path, encoding='utf-8', index=False)
    # 保存表格
    file_path.save()


def main():
    lis = []
    # for i in range(1,10):
    lis += get_Information('#医保满意#', 1)
    # print(lis)
    export_excel(lis)


if __name__ == '__main__':
    main()
    # pool = Pool()
    # groups = ([x*20 for x in range(GROUP_START,GROUP_END+1)])
    # pool.map(main,groups)
    # pool.close()
    # pool.join()

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:关键词 information Informatio connection formation

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
扫码
拉您进交流群
GMT+8, 2026-2-1 12:38