经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 编程经验 » 查看文章
微博-指定话题当日数据爬取
来源:cnblogs  作者:mingkai2004  时间:2024/6/12 20:46:53  对本文有异议

一、对微博页面的分析

(一)对微博网页端的分析

  1. 首先,我们打开微博,发现从电脑端打开微博,网址为:Sina Visitor System

image

  1. 我们搜索关键字:巴以冲突,会发现其对应的 URL:巴以冲突

image

(1)URL 编码/解码

通过对 URL 进行分析,不难发现我们输入的是中文“巴以冲突”,但是真实的链接却不含中文,这是因为链接中的中文被编码了。我们将复制来的 URL 进行解码操作便可以得知。

image

在巴以冲突这个页面里面可以看到高级搜索,打开高级搜索后发现可以对微博的发布时间进行筛选,还可以对微博类型、微博包含的内容进行筛选。 一开始,想的便是从这下手,非常方便爬取指定时间内指定话题下的微博内容。

image

(2)抓包分析请求网址/请求方法/响应内容

接着,打开开发者工具,对抓包进行分析,我们可以看到请求网址发生了变化, 请求网址为: https://weibo.com/ajax/side/search?q=%E5%B7%B4%E4%BB%A5%E5%86%B2%E7%AA%81请求方法是: GET

image

点开预览、查看相应内容,可以发现该请求网址返回的 json 文件内容对应的就是页面中加载的微博内容。

image

image

于是,便开始在 pycharm 中编写请求代码

  1. import requests # 导入requests库,用于发送HTTP请求
  2. url = 'https://weibo.com/ajax/side/search?q=%E5%B7%B4%E4%BB%A5%E5%86%B2%E7%AA%81'
  3. print(url)
  4. response = requests.get(url=url) # 发送HTTP GET请求
  5. print(f'响应状态码是:{response.status_code}') # 如果响应状态码为200(成功)
  6. print(response.json())

image


(二)网页端的局限性(cookie 、微博数量问题)

虽然状态码返回 200 表示成功,但是 json 文件里面只有很少的 50 条微博数据,这对于爬虫而言是非常少的数据。但是,当我们向下滑动想要进一步探究、查看更多数据时,会发现这时候微博官方不给我们查看,要求我们登陆账号后才能查看。

image

如果需要登陆账号才能查看更多微博内容,那么意味着在爬虫里面发送 http 请求时需要使用到账号的 cookie,又考虑到网站肯定存在对爬虫的检测,如果使用 cookie 的话,肯定会被封禁的,这不仅仅会影响爬取微博数据的效率,还会造成短时间内无法打开网站。

image

因此,这时候便不再尝试从当前网址下手爬取数据。


(三)微博手机端的分析

便开始在网上查阅相关的资料,想要找到一个无需 cookie 便能爬取微博数据内容,同时又能突破只能查看 50 条数据的局限性。最终,在某网页上面有网友提了一嘴,说:“在手机端界面爬取微博数据,比在网页端爬取更加方便、局限性相对来说更小”。 于是,便开始准备从手机端网址开始下手,先尝试着验证下网友说的是否正确。 手机端的网址是:https://m.weibo.cn

image

在搜索框内搜索巴以冲突,找到其对应的 URL:https://m.weibo.cn/search?containerid=100103type%3D1%26q%3D%E5%B7%B4%E4%BB%A5%E5%86%B2%E7%AA%81

image

(1)URL 编码/解码

不难发现,此处的中文仍然进行了编码操作,我们需要对链接进行解码查看是否为原来的 URL。

image

(2)抓包分析

打开开发者工具,开始对网页抓包进行分析,我们在 Fetch/XHR 里面可以找到 https 请求, 请求网址是:https://m.weibo.cn/api/container/getIndex?containerid=100103type%3D1%26q%3D%E5%B7%B4%E4%BB%A5%E5%86%B2%E7%AA%81&page_type=searchall, 请求方法是:GET, 请求参数是:containerid=100103type%3D1%26q%3D%E5%B7%B4%E4%BB%A5%E5%86%B2%E7%AA%81&page_type=searchall

image

(3)请求参数分析

image

由于微博每页都有数量限制,因此当下滑到一定程度时,又有新的微博内容显示,因此可以得知网页数据是通过ajax文件格式加载出来的。所以,找到其中的请求参数,发现存在page参数,这里 page的参数为 2,就是代表第二页。这个时候不难猜测出从本页面下手,并不存在微博内容数量的限制,我们只需要设置好 page 参数即可。

image

于是便开始写相关的代码,首先写好请求参数 params

  1. params = {
  2. 'containerid': f'100103type=1&q=#{keyword}#',
  3. 'page_type': 'searchall',
  4. 'page': page
  5. }

(4)json 内容分析

接着我们打开请求网址https://m.weibo.cn/api/container/getIndex?containerid=100103type%3D1%26q%3D%E5%B7%B4%E4%BB%A5%E5%86%B2%E7%AA%81&page_type=searchall,发现下面微博数据仍然是以 json 格式显示的,因此我们需要对 json 文件内容进行分析。

image

首先,我们将 json 文件内容复制到 json 格式化检验里,发现 返回的 json 是正确的 json 文件

image

为了方便对 json 文件内容的分析,我们选取 json 视图对 json 进行可视化分析。 不难发现,微博内容存在于 json/data/cards/里面,下图中的 0~22 均代表一条条微博内容等等数据。 image

(5)查看/提取 json 有效信息

结合网页内容对 cards 下面的内容进行分析,我们可以发现: -->在 mblog 中存在判断微博是否为长文本一项 isLongText : true 其他有用内容如下:

Json参数分析
  1. 'wid': item.get('id'), # 微博ID
  2. 'user_name': item.get('user').get('screen_name'), # 微博发布者名称
  3. 'user_id': item.get('user').get('id'), # 微博发布者ID
  4. 'gender': item.get('user').get('gender'), # 微博发布者性别
  5. 'publish_time': time_formater(item.get('created_at')), # 微博发布时间
  6. 'source': item.get('source'), # 微博发布来源
  7. 'status_province': item.get('status_province'), # 微博发布者所在省份
  8. 'text': pq(item.get("text")).text(), # 仅提取内容中的文本
  9. 'like_count': item.get('attitudes_count'), # 点赞数
  10. 'comment_count': item.get('comments_count'), # 评论数
  11. 'forward_count': item.get('reposts_count'), # 转发数

image

image

image

image

image

到了这里,我惊讶的发现爬取时并不需要用户的 cookie,可以证明网友的某些说法是正确的。


二、代码实现过程:

(1)导包

  1. import requests # 导入requests库,用于发送HTTP请求
  2. from urllib.parse import urlencode # 导入urlencode函数,用于构建URL参数
  3. import time # 导入time模块,用于添加时间延迟
  4. import random # 导入random模块,用于生成随机数
  5. from pyquery import PyQuery as pq # 导入PyQuery库,用于解析HTML和XML
  6. from datetime import datetime # 导入datetime模块,用于处理日期和时间
  7. import os
  8. import csv

(2)设置基础的 URL 以及请求参数 params

  1. # 设置代理等(新浪微博的数据是用ajax异步下拉加载的,network->xhr)
  2. host = 'm.weibo.cn' # 设置主机地址
  3. base_url = 'https://%s/api/container/getIndex?' % host # 基础URL,用于构建API请求URL
  4. user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' # 设置用户代理信息
  5. # 设置请求头
  6. headers = {
  7. 'Host': host, # 设置请求头中的Host字段
  8. 'keep': 'close', # 设置请求头中的keep字段
  9. # 话题巴以冲突下的URL对应的Referer
  10. # 'Referer': 'https://m.weibo.cn/search?containerid=100103type%3D1%26q%3D%E5%B7%B4%E4%BB%A5%E5%86%B2%E7%AA%81', #
  11. 'User-Agent': user_agent # 设置请求头中的User-Agent字段
  12. }

(3)将微博的时间格式转换为标准的日期时间格式

  1. # 用于将微博的时间格式转换为标准的日期时间格式
  2. def time_formater(input_time_str):
  3. input_format = '%a %b %d %H:%M:%S %z %Y' # 输入时间的格式
  4. output_format = '%Y-%m-%d %H:%M:%S' # 输出时间的格式
  5. return datetime.strptime(input_time_str, input_format).strftime(output_format)

(4)按页数 page 抓取微博内容数据

  1. # 按页数抓取数据
  2. def get_single_page(page, keyword):
  3. # https://m.weibo.cn/api/container/getIndex?containerid=100103type=1&q=巴以冲突&page_type=searchall&page=1
  4. # 构建请求参数
  5. params = {
  6. 'containerid': f'100103type=1&q=#{keyword}#',
  7. 'page_type': 'searchall',
  8. 'page': page
  9. }
  10. url = base_url + urlencode(params) # 将输入的中文关键词编码,构建出完整的API请求URL
  11. print(url) # 打印请求的URL
  12. error_times = 3 # 设置错误尝试次数
  13. while True:
  14. response = requests.get(url, headers=headers) # 发送HTTP GET请求
  15. if response.status_code == 200: # 如果响应状态码为200(成功)
  16. if len(response.json().get('data').get('cards')) > 0: # 检查是否有数据
  17. return response.json() # 返回JSON响应数据
  18. time.sleep(3) # 等待3秒
  19. error_times += 1 # 错误尝试次数增加
  20. if error_times > 3: # 如果连续出错次数超过3次
  21. return None # 返回空值

(5)定义长文本微博内容的爬取

  1. # 长文本爬取代码段
  2. def getLongText(lid): # 根据长文本的ID获取长文本内容
  3. # 长文本请求头
  4. headers_longtext = {
  5. 'Host': host,
  6. 'Referer': 'https://m.weibo.cn/status/' + lid,
  7. 'User-Agent': user_agent
  8. }
  9. params = {
  10. 'id': lid
  11. }
  12. url = 'https://m.weibo.cn/statuses/extend?' + urlencode(params) # 构建获取长文本内容的URL
  13. try:
  14. response = requests.get(url, headers=headers_longtext) # 发送HTTP GET请求
  15. if response.status_code == 200: # 如果响应状态码为200(成功)
  16. jsondata = response.json() # 解析JSON响应数据
  17. tmp = jsondata.get('data') # 获取长文本数据
  18. return pq(tmp.get("longTextContent")).text() # 解析长文本内容
  19. except:
  20. pass

(6)对 json 中的有效信息进行提取

  1. # 修改后的页面爬取解析函数
  2. def parse_page(json_data):
  3. global count # 使用全局变量count
  4. items = json_data.get('data').get('cards') # 获取JSON数据中的卡片列表
  5. for index, item in enumerate(items):
  6. if item.get('card_type') == 7:
  7. print('导语')
  8. continue
  9. elif item.get('card_type') == 8 or (item.get('card_type') == 11 and item.get('card_group') is None):
  10. continue
  11. if item.get('mblog', None):
  12. item = item.get('mblog')
  13. else:
  14. item = item.get('card_group')[0].get('mblog')
  15. if item:
  16. if item.get('isLongText') is False: # 不是长文本
  17. data = {
  18. 'wid': item.get('id'), # 微博ID
  19. 'user_name': item.get('user').get('screen_name'), # 微博发布者名称
  20. 'user_id': item.get('user').get('id'), # 微博发布者ID
  21. 'gender': item.get('user').get('gender'), # 微博发布者性别
  22. 'publish_time': time_formater(item.get('created_at')), # 微博发布时间
  23. 'source': item.get('source'), # 微博发布来源
  24. 'status_province': item.get('status_province'), # 微博发布者所在省份
  25. 'text': pq(item.get("text")).text(), # 仅提取内容中的文本
  26. 'like_count': item.get('attitudes_count'), # 点赞数
  27. 'comment_count': item.get('comments_count'), # 评论数
  28. 'forward_count': item.get('reposts_count'), # 转发数
  29. }
  30. else: # 长文本涉及文本的展开
  31. tmp = getLongText(item.get('id')) # 调用函数获取长文本内容
  32. data = {
  33. 'wid': item.get('id'), # 微博ID
  34. 'user_name': item.get('user').get('screen_name'), # 微博发布者名称
  35. 'user_id': item.get('user').get('id'), # 微博发布者ID
  36. 'gender': item.get('user').get('gender'), # 微博发布者性别
  37. 'publish_time': time_formater(item.get('created_at')), # 微博发布时间
  38. 'source': item.get('source'), # 微博发布来源
  39. 'text': tmp, # 仅提取内容中的文本
  40. 'status_province': item.get('status_province'), # 微博发布者所在省份
  41. 'like_count': item.get('attitudes_count'), # 点赞数
  42. 'comment_count': item.get('comments_count'), # 评论数
  43. 'forward_count': item.get('reposts_count'), # 转发数
  44. }
  45. count += 1
  46. print(f'total count: {count}') # 打印总计数
  47. yield data # 返回数据

(7)将爬取到的内容保存到 csv 文件内

  1. if __name__ == '__main__':
  2. keyword = '巴以冲突' # 设置关键词
  3. result_file = f'10月26日{keyword}话题.csv' # 设置结果文件名
  4. if not os.path.exists(result_file):
  5. with open(result_file, mode='w', encoding='utf-8-sig', newline='') as f:
  6. writer = csv.writer(f)
  7. writer.writerow(['微博ID', '微博发布者名称', '微博发布者ID', '微博发布者性别',
  8. '微博发布时间', '微博发布来源', '微博内容', '微博发布者所在省份', '微博点赞数量', '微博评论数量',
  9. '微博转发量']) # 写入CSV文件的标题行
  10. temp_data = [] # 用于临时存储数据的列表
  11. empty_times = 0 # 空数据的连续次数
  12. for page in range(1, 50000): # 循环抓取多页数据
  13. print(f'page: {page}')
  14. json_data = get_single_page(page, keyword) # 获取单页数据
  15. if json_data == None: # 如果数据为空
  16. print('json is none')
  17. break
  18. if len(json_data.get('data').get('cards')) <= 0: # 检查是否有数据
  19. empty_times += 1
  20. else:
  21. empty_times = 0
  22. if empty_times > 3: # 如果连续空数据超过3次
  23. print('\n\n consist empty over 3 times \n\n')
  24. break
  25. for result in parse_page(json_data): # 解析并处理页面数据
  26. temp_data.append(result) # 将数据添加到临时列表
  27. if page % save_per_n_page == 0: # 每隔一定页数保存一次数据
  28. with open(result_file, mode='a+', encoding='utf-8-sig', newline='') as f:
  29. writer = csv.writer(f)
  30. for d in temp_data:
  31. # 将爬取到的数据写入CSV文件
  32. writer.writerow(
  33. [d['wid'],
  34. d['user_name'],
  35. d['user_id'],
  36. d['gender'],
  37. d['publish_time'],
  38. d['source'],
  39. d['text'],
  40. d['status_province'],
  41. d['like_count'],
  42. d['comment_count'],
  43. d['forward_count']])
  44. print(f'\n\n------cur turn write {len(temp_data)} rows to csv------\n\n') # 打印保存数据的信息
  45. temp_data = [] # 清空临时数据列表
  46. time.sleep(random.randint(4, 8)) # 随机等待一段时间,模拟人的操作

(8)对csv数据进行处理

打开 csv 文件后会发现,存在重复的微博内容。 因为微博都有自己独有的 ID,故从 ID 下手对重复值进行删除处理。 在 Jupyter notebook 里面进行数据处理分析

  1. import pandas as pd
  2. # 读取CSV文件
  3. df = pd.read_csv('10月26日巴以冲突话题.csv')
  4. # 检测并删除重复值
  5. df.drop_duplicates(subset='微博ID', keep='first', inplace=True)
  6. # 保存处理后的结果到新的CSV文件
  7. df.to_csv('处理后的内容.csv', index=False)

三、完整的代码

完整爬虫代码(注释由gpt自动生成)
  1.  """
  2. 这段Python代码是一个用于爬取新浪微博数据的脚本。它使用了多个Python库来实现不同功能,包括发送HTTP请求、解析HTML和XML、处理日期和时间等。
  3. 以下是代码的主要功能和结构:
  4. 1. 导入所需的Python库:
  5. - `requests`: 用于发送HTTP请求。
  6. - `urllib.parse`: 用于构建URL参数。
  7. - `time`: 用于添加时间延迟。
  8. - `random`: 用于生成随机数。
  9. - `pyquery`: 用于解析HTML和XML。
  10. - `datetime`: 用于处理日期和时间。
  11. - `os`:用于文件操作。
  12. - `csv`:用于读写CSV文件。
  13. 2. 设置一些常量和请求头信息,包括主机地址、基础URL、用户代理信息、请求头等。
  14. 3. 定义了一个`time_formater`函数,用于将微博的时间格式转换为标准的日期时间格式。
  15. 4. 定义了一个`get_single_page`函数,用于按页数抓取数据,构建API请求URL,并发送HTTP GET请求。它还包含了错误重试逻辑。
  16. 5. 定义了一个`getLongText`函数,用于根据长文本的ID获取长文本内容。这部分代码涉及长文本的展开。
  17. 6. 定义了一个`parse_page`函数,用于解析页面返回的JSON数据,提取所需的信息,并生成数据字典。
  18. 7. 主程序部分包括以下功能:
  19. - 设置关键词(`keyword`)和结果文件名(`result_file`)。
  20. - 打开结果文件(CSV),如果文件不存在,则创建文件并写入标题行。
  21. - 定义临时数据列表`temp_data`,用于存储数据。
  22. - 进行循环,抓取多页数据,解析并处理页面数据,然后将数据写入CSV文件。
  23. - 在每隔一定页数保存一次数据到CSV文件。
  24. - 随机等待一段时间以模拟人的操作。
  25. 总体来说,这段代码的主要目的是爬取新浪微博中与特定关键词相关的微博数据,并将其保存到CSV文件中。
  26. 它处理了长文本的展开以及一些错误重试逻辑。需要注意的是,爬取网站数据时应遵守网站的使用政策和法律法规。
  27. """
  28. import requests # 导入requests库,用于发送HTTP请求
  29. from urllib.parse import urlencode # 导入urlencode函数,用于构建URL参数
  30. import time # 导入time模块,用于添加时间延迟
  31. import random # 导入random模块,用于生成随机数
  32. from pyquery import PyQuery as pq # 导入PyQuery库,用于解析HTML和XML
  33. from datetime import datetime # 导入datetime模块,用于处理日期和时间
  34. import os
  35. import csv
  36. # 设置代理等(新浪微博的数据是用ajax异步下拉加载的,network->xhr)
  37. host = 'm.weibo.cn' # 设置主机地址
  38. base_url = 'https://%s/api/container/getIndex?' % host # 基础URL,用于构建API请求URL
  39. user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36' # 设置用户代理信息
  40. # 设置请求头
  41. headers = {
  42. 'Host': host, # 设置请求头中的Host字段
  43. 'keep': 'close', # 设置请求头中的keep字段
  44. # 话题巴以冲突下的URL对应的Referer
  45. 'Referer': 'https://m.weibo.cn/search?containerid=100103type%3D1%26q%3D%E5%B7%B4%E4%BB%A5%E5%86%B2%E7%AA%81', #
  46. 'User-Agent': user_agent # 设置请求头中的User-Agent字段
  47. }
  48. save_per_n_page = 1 # 每隔多少页保存一次数据
  49. # 用于将微博的时间格式转换为标准的日期时间格式
  50. def time_formater(input_time_str):
  51. input_format = '%a %b %d %H:%M:%S %z %Y' # 输入时间的格式
  52. output_format = '%Y-%m-%d %H:%M:%S' # 输出时间的格式
  53. return datetime.strptime(input_time_str, input_format).strftime(output_format)
  54. # 按页数抓取数据
  55. def get_single_page(page, keyword):
  56. # https://m.weibo.cn/api/container/getIndex?containerid=100103type=1&q=巴以冲突&page_type=searchall&page=1
  57. # 构建请求参数
  58. params = {
  59. 'containerid': f'100103type=1&q=#{keyword}#',
  60. 'page_type': 'searchall',
  61. 'page': page
  62. }
  63. url = base_url + urlencode(params) # 将输入的中文关键词编码,构建出完整的API请求URL
  64. print(url) # 打印请求的URL
  65. error_times = 3 # 设置错误尝试次数
  66. while True:
  67. response = requests.get(url, headers=headers) # 发送HTTP GET请求
  68. if response.status_code == 200: # 如果响应状态码为200(成功)
  69. if len(response.json().get('data').get('cards')) > 0: # 检查是否有数据
  70. return response.json() # 返回JSON响应数据
  71. time.sleep(3) # 等待3秒
  72. error_times += 1 # 错误尝试次数增加
  73. if error_times > 3: # 如果连续出错次数超过3次
  74. return None # 返回空值
  75. # 长文本爬取代码段
  76. def getLongText(lid): # 根据长文本的ID获取长文本内容
  77. # 长文本请求头
  78. headers_longtext = {
  79. 'Host': host,
  80. 'Referer': 'https://m.weibo.cn/status/' + lid,
  81. 'User-Agent': user_agent
  82. }
  83. params = {
  84. 'id': lid
  85. }
  86. url = 'https://m.weibo.cn/statuses/extend?' + urlencode(params) # 构建获取长文本内容的URL
  87. try:
  88. response = requests.get(url, headers=headers_longtext) # 发送HTTP GET请求
  89. if response.status_code == 200: # 如果响应状态码为200(成功)
  90. jsondata = response.json() # 解析JSON响应数据
  91. tmp = jsondata.get('data') # 获取长文本数据
  92. return pq(tmp.get("longTextContent")).text() # 解析长文本内容
  93. except:
  94. pass
  95. # 解析页面返回的JSON数据
  96. count = 0 # 计数器,用于记录爬取的数据数量
  97. # 修改后的页面爬取解析函数
  98. def parse_page(json_data):
  99. global count # 使用全局变量count
  100. items = json_data.get('data').get('cards') # 获取JSON数据中的卡片列表
  101. for index, item in enumerate(items):
  102. if item.get('card_type') == 7:
  103. print('导语')
  104. continue
  105. elif item.get('card_type') == 8 or (item.get('card_type') == 11 and item.get('card_group') is None):
  106. continue
  107. if item.get('mblog', None):
  108. item = item.get('mblog')
  109. else:
  110. item = item.get('card_group')[0].get('mblog')
  111. if item:
  112. if item.get('isLongText') is False: # 不是长文本
  113. data = {
  114. 'wid': item.get('id'), # 微博ID
  115. 'user_name': item.get('user').get('screen_name'), # 微博发布者名称
  116. 'user_id': item.get('user').get('id'), # 微博发布者ID
  117. 'gender': item.get('user').get('gender'), # 微博发布者性别
  118. 'publish_time': time_formater(item.get('created_at')), # 微博发布时间
  119. 'source': item.get('source'), # 微博发布来源
  120. 'status_province': item.get('status_province'), # 微博发布者所在省份
  121. 'text': pq(item.get("text")).text(), # 仅提取内容中的文本
  122. 'like_count': item.get('attitudes_count'), # 点赞数
  123. 'comment_count': item.get('comments_count'), # 评论数
  124. 'forward_count': item.get('reposts_count'), # 转发数
  125. }
  126. else: # 长文本涉及文本的展开
  127. tmp = getLongText(item.get('id')) # 调用函数获取长文本内容
  128. data = {
  129. 'wid': item.get('id'), # 微博ID
  130. 'user_name': item.get('user').get('screen_name'), # 微博发布者名称
  131. 'user_id': item.get('user').get('id'), # 微博发布者ID
  132. 'gender': item.get('user').get('gender'), # 微博发布者性别
  133. 'publish_time': time_formater(item.get('created_at')), # 微博发布时间
  134. 'source': item.get('source'), # 微博发布来源
  135. 'text': tmp, # 仅提取内容中的文本
  136. 'status_province': item.get('status_province'), # 微博发布者所在省份
  137. 'like_count': item.get('attitudes_count'), # 点赞数
  138. 'comment_count': item.get('comments_count'), # 评论数
  139. 'forward_count': item.get('reposts_count'), # 转发数
  140. }
  141. count += 1
  142. print(f'total count: {count}') # 打印总计数
  143. yield data # 返回数据
  144. if __name__ == '__main__':
  145. keyword = '巴以冲突' # 设置关键词
  146. result_file = f'10月26日{keyword}话题.csv' # 设置结果文件名
  147. if not os.path.exists(result_file):
  148. with open(result_file, mode='w', encoding='utf-8-sig', newline='') as f:
  149. writer = csv.writer(f)
  150. writer.writerow(['微博ID', '微博发布者名称', '微博发布者ID', '微博发布者性别',
  151. '微博发布时间', '微博发布来源', '微博内容', '微博发布者所在省份', '微博点赞数量', '微博评论数量',
  152. '微博转发量']) # 写入CSV文件的标题行
  153. temp_data = [] # 用于临时存储数据的列表
  154. empty_times = 0 # 空数据的连续次数
  155. for page in range(1, 50000): # 循环抓取多页数据
  156. print(f'page: {page}')
  157. json_data = get_single_page(page, keyword) # 获取单页数据
  158. if json_data == None: # 如果数据为空
  159. print('json is none')
  160. break
  161. if len(json_data.get('data').get('cards')) <= 0: # 检查是否有数据
  162. empty_times += 1
  163. else:
  164. empty_times = 0
  165. if empty_times > 3: # 如果连续空数据超过3次
  166. print('\n\n consist empty over 3 times \n\n')
  167. break
  168. for result in parse_page(json_data): # 解析并处理页面数据
  169. temp_data.append(result) # 将数据添加到临时列表
  170. if page % save_per_n_page == 0: # 每隔一定页数保存一次数据
  171. with open(result_file, mode='a+', encoding='utf-8-sig', newline='') as f:
  172. writer = csv.writer(f)
  173. for d in temp_data:
  174. # 将爬取到的数据写入CSV文件
  175. writer.writerow(
  176. [d['wid'],
  177. d['user_name'],
  178. d['user_id'],
  179. d['gender'],
  180. d['publish_time'],
  181. d['source'],
  182. d['text'],
  183. d['status_province'],
  184. d['like_count'],
  185. d['comment_count'],
  186. d['forward_count']])
  187. print(f'\n\n------cur turn write {len(temp_data)} rows to csv------\n\n') # 打印保存数据的信息
  188. temp_data = [] # 清空临时数据列表
  189. time.sleep(random.randint(4, 8)) # 随机等待一段时间,模拟人的操作

原文链接:https://www.cnblogs.com/mingkai2004/p/18244624

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号