1

我想从 Quora 或带有代码的通用帖子中解析这篇文章。
示例:http: //qr.ae/Rkplrt

通过使用 Selenium,一个 Python 库,我可以获取帖子中的 HTML:

 h = html2text.HTML2Text()
 content = ans.find_element_by_class_name('inline_editor_value')
 html_string = content.get_attribute('innerHTML')
 text = h.handle(html_string)
 print text

我希望这一切都是一个文本块。但是对于那些包含代码的表,html2text 插入了很多\n并且不处理行的索引。

所以我可以看到:
https ://imageshack.com/i/paEKbzT4p (这是包含带有代码的表格的主要 div。) https://imageshack.com/i/hlIxFayop(html2text 提取的文本)
https ://imageshack.com/i/hlHFBXvQp(相反,这是文本的最终打印,索引行和额外\ns 存在问题。)

我已经尝试过不同的设置,例如 bypasse_tables,出现在 github 上的本指南中:( https://github.com/Alir3z4/html2text/blob/master/docs/usage.md#available-options ),但没有成功。

有人可以告诉我在这种情况下如何使用 html2text 吗?

4

2 回答 2

1

您可以使用BeautifulSoup(我urllib用来与网站通信,因为我不熟悉,selenium但我确信它可以工作)对 HTML 进行一些简单的解析:

import urllib
from bs4 import BeautifulSoup

# urllib opener
opener = urllib.request.build_opener(
          urllib.request.HTTPRedirectHandler(),
          urllib.request.HTTPHandler(debuglevel=0),
          urllib.request.HTTPSHandler(debuglevel=0))

# Get page
html = opener.open("http://qr.ae/Rkplrt").read()

# Create BeautifulSoup object
soup = BeautifulSoup(html, "lxml")

# Find the HTML element you want
answer = soup.find('div', { 'class' : 'ExpandedQText ExpandedAnswer' })

# Remove the stuff you don't want
answer.find('td', { 'class' : 'linenos' }).extract()
answer.find('div', { 'class' : 'ContentFooter AnswerFooter' }).extract()

# Print
print("\n".join(answer.stripped_strings))

我不完全确定您要提取什么。上面只给出了答案,包括代码,没有行号:

This is:
#include <stdio.h>
int v,i,j,k,l,s,a[99];
main()
{
for(scanf("%d", &s);*a-s;v=a[j*=v]-a[i],k=i<s,j+=(v=j<s&&(!k&&!!printf(2+"\n\n%c"-(!l<<!j)," #Q"[l^v?(l^j)&1:2])&&++l||a[i]<s&&v&&v-i+j&&v+i-j))&&!(l%=s),v||(i==j?a[i+=k]=0:++a[i])>=s*k&&++a[--i]);
}

更新: OP 要求<a><img>标签被它们的hrefsrc值替换。下面我的脚本版本应该解决这个问题。它还处理多个答案。

import urllib
from bs4 import BeautifulSoup

# urllib opener
opener = urllib.request.build_opener(
          urllib.request.HTTPRedirectHandler(),
          urllib.request.HTTPHandler(debuglevel=0),
          urllib.request.HTTPSHandler(debuglevel=0))

# Get page
html = opener.open("https://www.quora.com/Is-it-too-late-for-an-X-year-old-to-learn-how-to-program").read()

# Create BeautifulSoup object
soup = BeautifulSoup(html, "lxml")

# Place to store the final output
output = ''

# Find the HTML element you want
answers = soup.find_all('div', { 'class' : 'ExpandedQText ExpandedAnswer' })
for answer in answers:

  # Remove the stuff you don't want
  linenos = answer.find('td', { 'class' : 'linenos' })
  if linenos is not None:
    linenos.extract()
  answer.find('div', { 'class' : 'ContentFooter AnswerFooter' }).extract()

  # Replace <a> with its url
  for link in answer.select('a'):
    url = link['href']
    link.insert_after(url)
    link.extract()

  # Replace <a> with its url
  for img in answer.select('img'):
    url = img['src']
    img.insert_after(url)
    img.extract()

  # Attach to output
  output += "\n".join(answer.stripped_strings) + '\n\n'

# Print
print(output)
于 2015-10-27T17:26:45.437 回答
1

您实际上根本不需要使用HTML2Text

selenium可以直接给你“文本”:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://qr.ae/Rkplrt")

print(driver.find_element_by_class_name('inline_editor_content').text)

它打印帖子的内容:

The single line of code must be useful, not something meant to be confusing or obfuscating.

...

What examples have you created or encountered ?
于 2015-10-27T16:46:17.173 回答