我试图开发一个简单的网页刮板。我想提取没有HTML代码的文本。它适用于普通HTML,但不适用于JavaScript代码添加文本的某些页面。

例如,如果一些JavaScript代码添加了一些文本,我不能看到它,因为当我调用:

response = urllib2.urlopen(request)

我得到了原始文本而没有添加的文本(因为JavaScript是在客户端执行的)。

所以,我正在寻找一些解决这个问题的想法。


当前回答

尝试直接访问API

在抓取中常见的场景是网页从API端点异步请求数据。一个最小的例子是以下网站:

身体< > < >脚本 fetch(“https://jsonplaceholder.typicode.com/posts/1”) .then(res => { if (!res.ok)抛出错误(res.status); 返回res.json (); }) .then(data => { //页面加载后通过JS动态注入数据 document.body.innerText = data.title; }) .catch(err => console.error(err)) ; > < /脚本 身体< / >

在许多情况下,API将受到CORS或访问令牌的保护,或速率限制过高,但在其他情况下,它是公开可访问的,您可以完全绕过网站。对于CORS问题,你可以在任何地方尝试CORS。

一般的过程是使用浏览器的开发人员工具的网络选项卡来搜索页面发出的请求,以获得您想要抓取的数据的关键字/子字符串。通常,您会看到一个不受保护的API请求端点,该端点带有一个JSON有效负载,您可以直接使用urllib或请求模块访问该有效负载。上面的可运行代码片段就是这种情况,你可以用它来练习。点击“运行片段”后,下面是我如何在我的网络选项卡中找到端点:

这个例子是虚构的;从静态标记来看,端点URL可能不明显,因为它可以被动态组装、缩小并隐藏在数十个其他请求和端点之下。网络请求还将显示任何相关的请求有效负载细节,例如您可能需要的访问令牌。

在获取端点URL和相关细节后,使用标准HTTP库在Python中构建一个请求并请求数据:

>>> import requests
>>> res = requests.get("https://jsonplaceholder.typicode.com/posts/1")
>>> data = res.json()
>>> data["title"]
'sunt aut facere repellat provident occaecati excepturi optio reprehenderit'

当你可以摆脱它时,这往往比使用Selenium、Pyppeteer、Scrapy或其他流行的抓取库更容易、更快、更可靠。

如果您很不幸,数据没有通过API请求以良好的格式返回数据,那么它可能是原始浏览器负载的一部分,在<script>标记中,作为JSON字符串或(更可能的是)JS对象。例如:

<body> <script> var someHardcodedData = { 用户 ID: 1, 编号: 1, 题目: “Sunt aut facere repellat provident occaecati excepturi optio reprehenderit”, Body: 'quia et suscipit\nsuscipit recusandae con sequuntur expedita et\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto' }; document.body.textContent = someHardcodedData.title; </script> </body>

没有一种万能的方法来获取这些数据。基本技术是使用BeautifulSoup访问<script>标记文本,然后应用正则表达式或解析来提取对象结构、JSON字符串或数据可能采用的任何格式。下面是上面所示的示例结构的概念证明:

import json
import re
from bs4 import BeautifulSoup

# pretend we've already used requests to retrieve the data, 
# so we hardcode it for the purposes of this example
text = """
<body>
<script>
  var someHardcodedData = {
    userId: 1,
    id: 1,
    title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 
    body: 'quia et suscipit\nsuscipit recusandae con sequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'
  };
  document.body.textContent = someHardcodedData.title;
</script>
</body>
"""
soup = BeautifulSoup(text, "lxml")
script_text = str(soup.select_one("script"))
pattern = r"title: '(.*?)'"
print(re.search(pattern, script_text, re.S).group(1))

看看下面这些解析JS对象的资源,它们不是很有效的JSON:

如何将原始javascript对象转换为python字典? 如何修复JSON键值没有双引号?

以下是一些使用API绕过抓取的额外案例研究/概念证明:

如何使用Python beautifulsoup将yelp评论和星级评分刮到CSV Beautiful Soup对现有元素返回None 从BeautifulSoup Python中提取数据 通过POST收集Bandcamp粉丝(使用一种混合方法,即向网站发出初始请求,从使用BeautifulSoup的标记中提取一个令牌,然后在对JSON端点的第二个请求中使用)

如果所有这些都失败了,请尝试本线程中列出的许多动态抓取库中的一个。

其他回答

Pyppeteer

你可以考虑Pyppeteer,它是Chrome/Chromium驱动程序前端的Python移植版本。

下面是一个简单的例子,展示了如何使用pyppeterer动态地访问被注入到页面中的数据:

import asyncio
from pyppeteer import launch

async def main():
    browser = await launch({"headless": True})
    [page] = await browser.pages()

    # normally, you go to a live site...
    #await page.goto("http://www.example.com")
    # but for this example, just set the HTML directly:
    await page.setContent("""
    <body>
    <script>
    // inject content dynamically with JS, not part of the static HTML!
    document.body.innerHTML = `<p>hello world</p>`; 
    </script>
    </body>
    """)
    print(await page.content()) # shows that the `<p>` was inserted

    # evaluate a JS expression in browser context and scrape the data
    expr = "document.querySelector('p').textContent"
    print(await page.evaluate(expr, force_expr=True)) # => hello world

    await browser.close()

asyncio.get_event_loop().run_until_complete(main())

请参阅pyppeterer的参考文档。

你也可以使用webdriver执行javascript。

from selenium import webdriver

driver = webdriver.Firefox()
driver.get(url)
driver.execute_script('document.title')

或者将值存储在变量中

result = driver.execute_script('var text = document.title ; return text')

尝试直接访问API

在抓取中常见的场景是网页从API端点异步请求数据。一个最小的例子是以下网站:

身体< > < >脚本 fetch(“https://jsonplaceholder.typicode.com/posts/1”) .then(res => { if (!res.ok)抛出错误(res.status); 返回res.json (); }) .then(data => { //页面加载后通过JS动态注入数据 document.body.innerText = data.title; }) .catch(err => console.error(err)) ; > < /脚本 身体< / >

在许多情况下,API将受到CORS或访问令牌的保护,或速率限制过高,但在其他情况下,它是公开可访问的,您可以完全绕过网站。对于CORS问题,你可以在任何地方尝试CORS。

一般的过程是使用浏览器的开发人员工具的网络选项卡来搜索页面发出的请求,以获得您想要抓取的数据的关键字/子字符串。通常,您会看到一个不受保护的API请求端点,该端点带有一个JSON有效负载,您可以直接使用urllib或请求模块访问该有效负载。上面的可运行代码片段就是这种情况,你可以用它来练习。点击“运行片段”后,下面是我如何在我的网络选项卡中找到端点:

这个例子是虚构的;从静态标记来看,端点URL可能不明显,因为它可以被动态组装、缩小并隐藏在数十个其他请求和端点之下。网络请求还将显示任何相关的请求有效负载细节,例如您可能需要的访问令牌。

在获取端点URL和相关细节后,使用标准HTTP库在Python中构建一个请求并请求数据:

>>> import requests
>>> res = requests.get("https://jsonplaceholder.typicode.com/posts/1")
>>> data = res.json()
>>> data["title"]
'sunt aut facere repellat provident occaecati excepturi optio reprehenderit'

当你可以摆脱它时,这往往比使用Selenium、Pyppeteer、Scrapy或其他流行的抓取库更容易、更快、更可靠。

如果您很不幸,数据没有通过API请求以良好的格式返回数据,那么它可能是原始浏览器负载的一部分,在<script>标记中,作为JSON字符串或(更可能的是)JS对象。例如:

<body> <script> var someHardcodedData = { 用户 ID: 1, 编号: 1, 题目: “Sunt aut facere repellat provident occaecati excepturi optio reprehenderit”, Body: 'quia et suscipit\nsuscipit recusandae con sequuntur expedita et\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto' }; document.body.textContent = someHardcodedData.title; </script> </body>

没有一种万能的方法来获取这些数据。基本技术是使用BeautifulSoup访问<script>标记文本,然后应用正则表达式或解析来提取对象结构、JSON字符串或数据可能采用的任何格式。下面是上面所示的示例结构的概念证明:

import json
import re
from bs4 import BeautifulSoup

# pretend we've already used requests to retrieve the data, 
# so we hardcode it for the purposes of this example
text = """
<body>
<script>
  var someHardcodedData = {
    userId: 1,
    id: 1,
    title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 
    body: 'quia et suscipit\nsuscipit recusandae con sequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'
  };
  document.body.textContent = someHardcodedData.title;
</script>
</body>
"""
soup = BeautifulSoup(text, "lxml")
script_text = str(soup.select_one("script"))
pattern = r"title: '(.*?)'"
print(re.search(pattern, script_text, re.S).group(1))

看看下面这些解析JS对象的资源,它们不是很有效的JSON:

如何将原始javascript对象转换为python字典? 如何修复JSON键值没有双引号?

以下是一些使用API绕过抓取的额外案例研究/概念证明:

如何使用Python beautifulsoup将yelp评论和星级评分刮到CSV Beautiful Soup对现有元素返回None 从BeautifulSoup Python中提取数据 通过POST收集Bandcamp粉丝(使用一种混合方法,即向网站发出初始请求,从使用BeautifulSoup的标记中提取一个令牌,然后在对JSON端点的第二个请求中使用)

如果所有这些都失败了,请尝试本线程中列出的许多动态抓取库中的一个。

这似乎是一个很好的解决方案,从一个伟大的博客文章

import sys  
from PyQt4.QtGui import *  
from PyQt4.QtCore import *  
from PyQt4.QtWebKit import *  
from lxml import html 

#Take this class for granted.Just use result of rendering.
class Render(QWebPage):  
  def __init__(self, url):  
    self.app = QApplication(sys.argv)  
    QWebPage.__init__(self)  
    self.loadFinished.connect(self._loadFinished)  
    self.mainFrame().load(QUrl(url))  
    self.app.exec_()  

  def _loadFinished(self, result):  
    self.frame = self.mainFrame()  
    self.app.quit()  

url = 'http://pycoders.com/archive/'  
r = Render(url)  
result = r.frame.toHtml()
# This step is important.Converting QString to Ascii for lxml to process

# The following returns an lxml element tree
archive_links = html.fromstring(str(result.toAscii()))
print archive_links

# The following returns an array containing the URLs
raw_links = archive_links.xpath('//div[@class="campaign"]/a/@href')
print raw_links

也许硒可以做到。

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get(url)
time.sleep(5)
htmlSource = driver.page_source