Python爬取微博话题全部内容

代码还存在缺陷,无法获取全部的话题内容,爬取结果只有10条,请帮忙修改完善一下。

img

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import time
import os

chrome_driver_path = "F:\死磕\stance分析\chromedriver.exe"
driver = webdriver.Chrome(executable_path=chrome_driver_path)

weibo_url = "https://s.weibo.com/weibo?q=%23chatgpt%23"
driver.get(weibo_url)

# 等待登录按钮加载出来
wait = WebDriverWait(driver, 10)
login_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//a[text()='登录']")))

# 点击登录按钮
login_button.click()

# 等待登录完成
time.sleep(10)  # 假设需要等待10秒钟登录完成,你可以根据实际情况调整等待时间

# 切回主页面
driver.switch_to.default_content()

# 模拟滚动加载内容
scroll_pause_time = 2.0
scroll_limit = 20  # 假设滚动20次

scrolls = 0
while scrolls < scroll_limit:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(scroll_pause_time)
    scrolls += 1

# 解析页面内容
page_source = driver.page_source

# 使用 Beautiful Soup 解析页面内容
from bs4 import BeautifulSoup

soup = BeautifulSoup(page_source, "html.parser")

# 查找博文内容
posts = soup.find_all("p", class_="txt")

# 设置保存目录
save_dir = "F:\死磕\stance分析\结果"

# 创建保存目录
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

# 逐条保存博文内容为 txt 文件
for i, post in enumerate(posts):
    post_text = post.get_text(strip=True)
    file_path = os.path.join(save_dir, f"weibo_{i + 1}.txt")

    with open(file_path, "w", encoding="utf-8") as file:
        file.write(post_text)

    print(f"保存第{i + 1}条微博至 {file_path}")

# 关闭浏览器
driver.quit()

print("爬取和保存完成!")

看了一下你的代码,你模拟出来的是PC端的网页,是点击下一页来翻页的,你滚动翻页根本不起作用。
所以,你直接找到那个下一页的按钮,爬完当前页面后,点击一下下一页,然后继续爬,就可以了。

不过我观察了一下,我发现这个页面的url是可以直接增加页数参数的,所以不用找那个按钮也行,你就直接

for p in range(1,50): 
    weibo_url = "https://s.weibo.com/weibo?q=%23chatgpt%23&page="+str(p)
    driver.get(weibo_url)
    #这里按照原来的逻辑处理爬取即可。

这样访问,然后在每个页面中按照你的逻辑直接爬取应该就可以了。你试试。

在你提供的代码中,只爬取到了10条微博内容,是因为你设置了一个滚动限制(scroll_limit = 20),导致只滚动了20次。你可以把这个值设置的再大一点,或者可以设置一个无限滚动while一下就好了。


如果以上回答对您有所帮助,点击一下采纳该答案~谢谢



from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import os
# 使用 Beautiful Soup 解析页面内容
from bs4 import BeautifulSoup

# 初始化 Chrome 浏览器
driver = webdriver.Chrome()

# 打开微博登录页面
login_url = "https://passport.weibo.cn/signin/login"
driver.get(login_url)

wait = WebDriverWait(driver, 10)

# 输入用户名和密码并点击登录按钮
username_input = wait.until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="loginName"]'))
)

password_input = wait.until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="loginPassword"]'))
)

username_input.send_keys("")  # 替换为实际的用户名
password_input.send_keys("")  # 替换为实际的密码

login_button = wait.until(
    EC.element_to_be_clickable((By.XPATH, "//*[@id='loginAction']"))
)
login_button.click()

verfy_btn = wait.until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="vdVerify"]/div[1]/div/div/div[3]/a'))
)
verfy_btn.click()

shoujicode=input('请输入手机验证码: ')
time.sleep(30)

verfy_cd = wait.until(
    EC.element_to_be_clickable((By.XPATH, '#//*[@id="verifyCode"]/div[1]/div/div/div[2]/div/div/div'))
).send_keys(shoujicode)

verfy_sub = wait.until(
    EC.element_to_be_clickable((By.XPATH, '//*[@id="verifyCode"]/div[1]/div/div/div[3]/a'))
).click()


# 打印当前页面标题,检查是否登录成功
print(driver.title)

# 在这里可以进行判断是否登录成功,如果成功则保存Cookie
if "微博" in driver.title:
    cookies = driver.get_cookies()
    with open('cookies.pkl', 'wb') as f:
        import pickle
        pickle.dump(cookies, f)

# 等待页面加载完成
time.sleep(5)

# 打开微博页面,此处以某个话题页为例
weibo_url = "https://s.weibo.com/weibo?q=%23%E5%87%89%E6%8B%8C%E9%BB%84%E7%93%9C%23"
driver.get(weibo_url)

# 等待页面加载完成
time.sleep(5)

# 解析页面内容
page_source = driver.page_source



soup = BeautifulSoup(page_source, "html.parser")

# 查找博文内容
posts = soup.find_all("p", class_="txt")

# 设置保存目录
save_dir = "/Users/liuzhenrong/Downloads/weibo_results/"

# 创建保存目录
if not os.path.exists(save_dir):
    os.makedirs(save_dir)

# 逐条保存博文内容为 txt 文件
for i, post in enumerate(posts):
    post_text = post.get_text(strip=True)
    file_path = os.path.join(save_dir, f"weibo_{i + 1}.txt")

    with open(file_path, "w", encoding="utf-8") as file:
        file.write(post_text)

    print(f"保存第{i + 1}条微博至 {file_path}")

# 模拟滚动页面以加载更多微博
for _ in range(3):  # 例如滚动三次
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(3)  # 等待加载

    # 解析加载后的页面内容
    page_source = driver.page_source
    soup = BeautifulSoup(page_source, "html.parser")
    new_posts = soup.find_all("p", class_="txt")

    # 继续保存博文内容为 txt 文件
    for j, post in enumerate(new_posts):
        post_text = post.get_text(strip=True)
        file_path = os.path.join(save_dir, f"weibo_{i + len(posts) + j + 1}.txt")  # 注意索引需要调整

        with open(file_path, "w", encoding="utf-8") as file:
            file.write(post_text)

        print(f"保存第{i + len(posts) + j + 1}条微博至 {file_path}")

# 关闭浏览器
driver.quit()

print("爬取和保存完成!")

【以下回答由 GPT 生成】

对于这个问题,有几个潜在的原因导致你只能获取到部分微博话题内容。以下是一些可能的解决步骤:

  1. 使用更长的等待时间: 在登录完成之后,你可以尝试增加等待的时间,以确保页面加载完全。你可以修改以下代码行来增加等待时间:
time.sleep(10)  # 假设需要等待10秒钟登录完成,你可以根据实际情况调整等待时间
  1. 增加滚动次数: 假设滚动20次不足以获取到全部的内容,你可以尝试增加滚动次数。你可以修改以下代码行来增加滚动次数:
scroll_limit = 20  # 假设滚动20次
  1. 将滚动操作放在循环外部: 目前的代码将滚动操作放在了while循环内部,这可能导致重复加载相同的内容。你可以将滚动操作放在循环外部,使其只执行一次,如下所示:
scroll_limit = 20  # 假设滚动20次

# 执行滚动操作
for _ in range(scroll_limit):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(scroll_pause_time)
  1. 检查页面解析方法: 使用Beautiful Soup解析页面内容可能会出现问题,导致无法获取到全部的内容。你可以尝试使用其他的页面解析方法或库,例如XPath或lxml。

  2. 使用API:微博提供了官方的API,你可以通过调用API来获得更完整的微博话题内容。你需要注册一个开发者账号并获取API密钥,然后使用Python的requests库发送API请求,接收响应并解析数据。关于如何使用微博API,可以参考微博提供的文档。

希望这些优化方法能够帮助你获取到全部的微博话题内容。如果还有其他问题,请随时提问。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^