鱼C论坛

 找回密码
 立即注册
查看: 1969|回复: 7

python怎么实现获取下拉后页面加载的内容?

[复制链接]
发表于 2020-5-27 21:29:00 | 显示全部楼层 |阅读模式
1鱼币
steam评测页面下拉后每次出现十条新内容,代码运行时无法获取下拉后的加载内容,求教
  1. import requests
  2. from bs4 import BeautifulSoup
  3. import json
  4. import time


  5. # from selenium import webdriver
  6. #
  7. # driver = webdriver.Chrome(r'C:\Users\m1359\AppData\Local\Google\Chrome\Application\chromedriver.exe')

  8. def sen_from_text(text):
  9.     SENTIMENT_URL = 'http://api.bosonnlp.com/sentiment/analysis'
  10.     h = {'X-Token': 'balbala'}  # your token
  11.     data = json.dumps(text)
  12.     resp = requests.post(SENTIMENT_URL, headers=h, data=data.encode('utf-8'))
  13.     resp = json.loads(resp.text)  # print(resp)
  14.     front = float(resp[0][0])
  15.     return front


  16. headers = {"Accept-Language": "zh-CN,zh;q=0.9"}

  17. file = open('steam.txt', 'w+', encoding='utf-8')
  18. for i in range(1, 10): # 动态获取评测页面
  19.     url = 'http://steamcommunity.com/app/578080/homecontent/?userreviewsoffset=' + str(10 * (i - 1)) + '&p=' + str(
  20.         i) + '&workshopitemspage=' + str(i) + '&readytouseitemspage=' + str(i) + '&mtxitemspage=' + str(
  21.         i) + '&itemspage=' + str(i) + '&screenshotspage=' + str(i) + '&videospage=' + str(i) + '&artpage=' + str(
  22.         i) + '&allguidepage=' + str(i) + '&webguidepage=' + str(i) + '&integratedguidepage=' + str(
  23.         i) + '&discussionspage=' + str(
  24.         i) + '&numperpage=10&browsefilter=toprated&browsefilter=toprated&appid=578080&l=schinese&appHubSubSection=10' \
  25.              '&filterLanguage=default&searchText=&forceanon=1 ' # steam评测页面的url,参考链接[url=https://www.tinymind.net.cn/articles/6c517fc1b33931]https://www.tinymind.net.cn/articles/6c517fc1b33931[/url]写的

  26.     html = requests.get(url, headers=headers, timeout=10).text
  27.     soup = BeautifulSoup(html, 'html.parser')  
  28.     reviews = soup.find_all('div', {'class': "apphub_Card modalContentLink interactable"})

  29.     for review in reviews:
  30.         # nick = review.find('div', {'class': 'apphub_CardContentAuthorName offline ellipsis'})
  31.         title = review.find('div', {'class': 'title'}).text # 获取评价(推荐or不推荐)
  32.         hour = review.find('div', {'class': 'hours'}).text.split(' ')[1] # 获取游玩时间
  33.         link = review.find('a').attrs['href'] # 获取用户主页链接
  34.         comment = review.find('div', {'class': 'apphub_CardTextContent'}).text.split('\n')[2].strip('\t') # 获取评价内容
  35.         # sen = sen_from_text(comment)
  36.         print(title, hour, link, comment)
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-27 21:36:29 | 显示全部楼层
下拉一般是ajax,【浏览器f12】-【网络】-【xhr】-【下拉一次】-【学ajax的头模拟get】
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-27 21:44:43 | 显示全部楼层
本帖最后由 Twilight6 于 2020-5-27 21:52 编辑

下拉页面后加载的内容是 ajax 现象

看这个视频教程吧,这个是爬取豆瓣下拉时刷新页面的案例

https://www.bilibili.com/video/BV1z541167mu?p=8
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-27 22:14:51 | 显示全部楼层
suchocolate 发表于 2020-5-27 21:36
下拉一般是ajax,【浏览器f12】-【网络】-【xhr】-【下拉一次】-【学ajax的头模拟get】

就是模拟的那个url
  1. Request URL: https://steamcommunity.com/app/578080/homecontent/?userreviewscursor=AoIFP%2FU1fmAAAABwo565AQ%3D%3D&userreviewsoffset=10&p=2&workshopitemspage=2&readytouseitemspage=2&mtxitemspage=2&itemspage=2&screenshotspage=2&videospage=2&artpage=2&allguidepage=2&webguidepage=2&integratedguidepage=2&discussionspage=2&numperpage=10&browsefilter=toprated&browsefilter=toprated&l=schinese&appHubSubSection=10&filterLanguage=default&searchText=
复制代码

我是按照文章里的方法拼接的,应该是没有问题的吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-28 07:57:15 | 显示全部楼层
诶呀二傻子是我 发表于 2020-5-27 22:14
就是模拟的那个url

我是按照文章里的方法拼接的,应该是没有问题的吧

照着实际的url拼接应该没问题,不过ajax的head也得写全,最好是原封不动复制head。ajax的head里头最重要的参数是"X-Requested-With": "XMLHttpRequest"
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-5-28 10:14:58 From FishC Mobile | 显示全部楼层
用requests-html直接模拟下拉操作更简单
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-2 10:57:37 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-6-7 08:09:25 | 显示全部楼层
  1. driver.find_element_by_xpath('//li[text()="xxx"]').click()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 21:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表