【问题标题】:How do I extract the text content of an article site with Python 3? [duplicate]如何使用 Python 3 提取文章站点的文本内容? [复制]
【发布时间】:2017-09-15 18:06:06
【问题描述】:

我尝试了以下方法:

import urllib

link = 'https://automatetheboringstuff.com/chapter7/'
f = urllib.request.urlopen(link)
myfile = f.read()
print(myfile)

但这似乎只是返回页面的源而不是文本内容。

【问题讨论】:

  • 你需要BeautifulSoup
  • 正确吗urllib.request.urlopen(link)

标签: python python-3.x text extract urllib


【解决方案1】:

如果你只想获得章节文本,我认为美汤是你的选择。

在你的情况下:

import requests
from bs4 import BeautifulSoup

res = requests.get('https://automatetheboringstuff.com/chapter7/')
soup = BeautifulSoup(res.text, 'html.parser')
print(soup.find('div', { "class" : "book" }).text)

【讨论】:

    猜你喜欢
    • 2017-02-18
    • 2015-07-04
    • 1970-01-01
    • 2014-10-19
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    相关资源
    最近更新 更多