【问题标题】:Scraping Kickstarter Project Page with Python使用 Python 抓取 Kickstarter 项目页面
【发布时间】:2021-10-20 19:15:43
【问题描述】:

一年多以来,我一直在使用下面的代码来抓取某些 Kickstarter 页面,这是我日常工作的一部分。没有恶意或恶意,只需要从页面中获取一些信息来帮助项目创建者。

但是在过去的 4 到 6 个月里,Kickstarter 实施了某种阻止程序,它阻止我访问/抓取实际页面。我得到的只是Backer or bot?Complete this security check to prove that you’re a human. Once you’ve passed this page, you might need to navigate away from your current screen on Kickstarter to refresh and move on.To avoid seeing this page again, double-check that JavaScript and cookies are enabled on your web browser and that you’re not blocking them from loading with an extension (e.g., ad blockers).

任何人都可以想出一种方法来绕过此检查并实际登陆页面吗?任何输入都会非常有帮助。

import os
import sys
import requests
import time
import urllib
import urllib.request
import shutil
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from csv import writer
from shutil import copyfile

print('What is the project URL?')
urlInp = input()

elClass = "rte__content"

chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_options)

driver.get(urlInp)
time.sleep(2)
html = driver.execute_script("return document.documentElement.outerHTML")
driver.quit()

soup = BeautifulSoup(html, 'lxml')
ele = soup.find('div', {'class': elClass})

print(soup)
quit()

【问题讨论】:

  • 你希望我们做什么??
  • 我希望有人遇到过这个问题并找到了解决方案。远射我知道,但希望。

标签: python-3.x selenium web-scraping beautifulsoup kickstarter


【解决方案1】:

看看你的剧本 - 看起来你正试图了解故事。

Selenium 非常适合 GUI 测试,但它会向网站宣布它是谁,以帮助防止 DOS 攻击。如果您想了解更多信息,请阅读 the docs 的更多信息。我认为这些网站出于某种原因正在努力阻止 GUI 自动化。他们有很多聪明的人在努力,所以想要打败他们将是一场艰苦的战斗。

您是否考虑过使用requests 库作为更好的选择? - 这将允许您在不需要浏览器的情况下模拟呼叫

我查看了开发工具,甚至还有一个 API 可以为您获取故事信息。您需要一个csrf token,并且您需要发布一些数据(这些数据已经在您的 url 中可用)。这将比 selenium 运行得更快,并允许您做更多事情。

这是我为您整理的一些代码。我选择了一个随机的 kickstarter 页面,它被硬编码到这个演示中:

urlInp = 'https://www.kickstarter.com/projects/iamlunasol/soft-like-mochi-enamel-pins?ref=section-homepage-featured-project'


#start a session - this stores cookies
s = requests.session()

# go here to get  cookies and the token
landing = s.get(urlInp) 
page = html.fromstring(landing.content)
csrf = page.xpath('//meta[@name="csrf-token"]')[0].get('content')
headers={} 
headers['x-csrf-token'] = csrf


#hit the api with the data
graphslug = urlInp.split("projects/")[1]
graphslug = graphslug.split("?")[0]
graphData= [{
        "operationName": "Campaign",
        "variables": {
            "slug": graphslug
        },
        "query": "query Campaign($slug: String!) {\n  project(slug: $slug) {\n    id\n    isSharingProjectBudget\n    risks\n    showRisksTab\n    story(assetWidth: 680)\n    currency\n    spreadsheet {\n      displayMode\n      public\n      url\n      data {\n        name\n        value\n        phase\n        rowNum\n        __typename\n      }\n      dataLastUpdatedAt\n      __typename\n    }\n    environmentalCommitments {\n      id\n      commitmentCategory\n      description\n      __typename\n    }\n    __typename\n  }\n}\n"
    }]

response = s.post("https://www.kickstarter.com/graph", json=graphData, headers=headers)

#process the response
graph_json = response.json()
story = graph_json[0]['data']['project']['story']
soup = BeautifulSoup(story, 'lxml')
print(soup)

输出的前几行是:

<html><body><p>Hi! I'm Felice Regina (<a href="https://www.instagram.com/iamlunasol/" rel="noopener" target="_blank">@iamlunasol</a> on Instagram) but everyone just calls me Luna! I'm an independent illustrator and pin designer! I've run many successful 
Kickstarter campaigns for enamel pins over the past few years. This campaign will help put new hard enamel pin designs into production.</p>
<p>Pledging ensures that the pins get produced, discounts when you purchase multiple pins, plus any freebies that we may unlock. If the campaign is successful, any extra pins will be sold at $12 + shipping in my <a href="https://shopiamlunasol.com/" rel="noopener" target="_blank">web store</a>.</p>

这与在 devtools 上的 json 中看到的 story 相关联 - 预览选项卡对此很有用:

最后,如果您希望对其进行调整以使用其他查询,您可以了解要从请求有效负载中的标头选项卡发送的 json 数据:

【讨论】:

  • 谢谢里奇。这个周末我会试一试。以前,他们经常更改 div 类和 ID 名称,以阻止任何爬网该站点的机器人 - 我假设。再次感谢。
  • 谢谢里奇。从您上面的示例中,我能够使其满足我的需求。非常感谢的人。
  • 嗨,Rich,很抱歉重新提出这个老问题,但几周前我遇到了一个错误,似乎无法弄清楚发生了什么(很可能是 Kickstarter 更改)。因此,在“csrf=”行上,它似乎正在中断并且没有超越这一点。如果你有机会,你能看到问题可能是什么吗?我搜索了开发工具,但看不到对“csrf-token”的引用。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 2013-04-29
  • 2021-06-09
  • 1970-01-01
  • 2022-11-17
相关资源
最近更新 更多