- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Python版本:2.7.10
我的代码:
# -*- coding: utf-8 -*-
from urllib2 import urlopen
from bs4 import BeautifulSoup
from collections import OrderedDict
import re
import string
def cleanInput(input):
input = re.sub('\n+', " ", input)
input = re.sub('\[[0-9]*\]', "", input)
input = re.sub(' +', " ", input)
# input = bytes(input, "UTF-8")
input = bytearray(input, "UTF-8")
input = input.decode("ascii", "ignore")
cleanInput = []
input = input.split(' ')
for item in input:
item = item.strip(string.punctuation)
if len(item) > 1 or (item.lower() == 'a' or item.lower() == 'i'):
cleanInput.append(item)
return cleanInput
def ngrams(input, n):
input = cleanInput(input)
output = []
for i in range(len(input)-n+1):
output.append(input[i:i+n])
return output
url = 'https://en.wikipedia.org/wiki/Python_(programming_language)'
html = urlopen(url)
bsObj = BeautifulSoup(html, 'lxml')
content = bsObj.find("div", {"id": "mw-content-text"}).get_text()
ngrams = ngrams(content, 2)
keys = range(len(ngrams))
ngramsDic = {}
for i in range(len(keys)):
ngramsDic[keys[i]] = ngrams[i]
# ngrams = OrderedDict(sorted(ngrams.items(), key=lambda t: t[1], reverse=True))
ngrams = OrderedDict(sorted(ngramsDic.items(), key=lambda t: t[1], reverse=True))
print ngrams
print "2-grams count is: " + str(len(ngrams))
我最近按照书 Web Scraping with Python: Collecting Data from the Modern Web 学习如何进行网页抓取,而在第 7 章数据规范化部分中,我首先按照书中显示的方式编写代码,并从终端收到错误:
Traceback (most recent call last):
File "2grams.py", line 40, in <module>
ngrams = OrderedDict(sorted(ngrams.items(), key=lambda t: t[1], reverse=True))
AttributeError: 'list' object has no attribute 'items'
因此,我通过创建一个新字典来更改代码,其中实体是 ngrams
列表。但我得到了完全不同的结果:
问题:
[u'Software', u'Foundation']
出现了37次,但没有出现40次。造成这种差异的原因是什么(可能是我的代码错误)?书籍截图:
最佳答案
本章也出现错误,因为 ngrams 是一个列表。我将它转换为字典并且它有效
ngrams1 = OrderedDict(sorted(dict(ngrams1).items(), key=lambda t: t[1], reverse=True))
关于python - 如何获得书 "Web Scraping with Python: Collecting Data from the Modern Web"第 7 章数据标准化部分中的相同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811790/
我的英语很差,抱歉 这是我的结构: bookstore ---author(app1) ---book(app2) 或者在代码中: from django.db import models from
我对antlr 的理解停留在非常基础的层面。浏览 Parr 博士的“权威 ANTLR 4 引用”。在第 4.2 节“使用访问者构建计算器”中列出了以下语法: grammar LabeledExpr;
我对antlr 的理解停留在非常基础的层面。浏览 Parr 博士的“权威 ANTLR 4 引用”。在第 4.2 节“使用访问者构建计算器”中列出了以下语法: grammar LabeledExpr;
我正在学习 1.1.0 的 rust book 教程,但尝试运行他们的代码时出现错误。 我有以下内容: extern crate rand; use std::io; use std::cmp::Or
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我的帖子是书的代码。 void qsort(void *lineptr[], int left, int right, int (*comp)(void *, void *)); /*Why it
考虑到 Fluent NHibernate 已经有一段时间了,我想应该会有一本书可以买到,所以我在亚马逊和谷歌中搜索,但没有关于 fluent nhiberanet 的书籍。我是对的还是这本书已经存在
考虑到 DocBook 格式的书可以以“模块化”的方式完成,我希望我可以用 AsciiDoc 做类似的事情,并将章节和第一级部分拆分到单独的文件中。不幸的是,文档没有提及这一点。到目前为止,我看到的唯
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我目前正在使用 Jquery 使用 turn.js 制作一本书,进展顺利,但我需要一点帮助。你看,我正在尝试制作一本精装期刊类型的书,就像此处显示的示例一样 http://www.turnjs.com
我正在阅读 Bjarne S 的 C++ 编程语言。 在第 77 页,第 4.8 节中,我发现了这一点: "枚举器可以用常量表达式初始化(§C.5) 的整数类型 (§4.1.1)。这枚举的范围包含向上
为什么当我输入由空格分隔的字符串(在 while 循环之外)并且我尝试在屏幕上打印它们时,只有我输入的第一个出现,而在这个 while 循环中(见代码)它打印所有这些一个? //this one pr
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在读这本书并被困在这里: public static class EventArgExtensions { public static void Raise(this TEventArgs
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在尝试使用 xlwt 创建具有多个选项卡的输出文件(.xlsx 格式)。我的Python版本号是2.7,IDE是Aptana Studio 3。 我以前使用过 xlwt 包,在相同的环境下执行相同
如何使用 Xcode 或 macOS 打开 Swift Playground Book?当我打开它时,我得到了这个: 最佳答案 在 Xcode 上,转到 Navigate > Reveal in Pr
嗨,我对 php 很陌生,我正在阅读 Murach PHP 书。我在单击类别部分中的链接时遇到问题,因为当它单击该链接时,它会广告 ?category_id=3 但问题是它在 add_product.
我是一名优秀的程序员,十分优秀!