当前位置: 首页 > 面试题库 >

Python HTML解析,提供漂亮的汤和过滤停用词

咸臻
2023-03-14
问题内容

我正在将网站的特定信息解析为文件。现在,我所拥有的程序将查看一个网页,并找到正确的HTML标签并解析出正确的内容。现在,我想进一步过滤这些“结果”。

例如,在网站上:http : //allrecipes.com/Recipe/Slow-Cooker-Pork-Chops-
II/Detail.aspx

我正在解析位于

标记中的成分。这个解析器很好地完成了这项工作,但是我想进一步处理这些结果。

当我运行此解析器时,它将删除数字,符号,逗号和斜杠(\或/),但保留所有文本。当我在网站上运行它时,会得到如下结果:

cup olive oil
cup chicken broth
cloves garlic minced
tablespoon paprika

现在,我想通过删除诸如“ cup”,“丁香”,“剁碎”,“
tablesoon”等停用词来进一步处理此问题。我到底该怎么做?这段代码是用python编写的,我不是很擅长,我只是使用这个解析器来获取可以手动输入的信息,但我宁愿不这样做。

任何有关如何详细执行此操作的帮助将不胜感激!我的代码如下:我该怎么做?

码:

import urllib2
import BeautifulSoup

def main():
    url = "http://allrecipes.com/Recipe/Slow-Cooker-Pork-Chops-II/Detail.aspx"
    data = urllib2.urlopen(url).read()
    bs = BeautifulSoup.BeautifulSoup(data)

    ingreds = bs.find('div', {'class': 'ingredients'})
    ingreds = [s.getText().strip('123456789.,/\ ') for s in ingreds.findAll('li')]

    fname = 'PorkRecipe.txt'
    with open(fname, 'w') as outf:
        outf.write('\n'.join(ingreds))

if __name__=="__main__":
    main()

问题答案:
import urllib2
import BeautifulSoup
import string

badwords = set([
    'cup','cups',
    'clove','cloves',
    'tsp','teaspoon','teaspoons',
    'tbsp','tablespoon','tablespoons',
    'minced'
])

def cleanIngred(s):
    # remove leading and trailing whitespace
    s = s.strip()
    # remove numbers and punctuation in the string
    s = s.strip(string.digits + string.punctuation)
    # remove unwanted words
    return ' '.join(word for word in s.split() if not word in badwords)

def main():
    url = "http://allrecipes.com/Recipe/Slow-Cooker-Pork-Chops-II/Detail.aspx"
    data = urllib2.urlopen(url).read()
    bs = BeautifulSoup.BeautifulSoup(data)

    ingreds = bs.find('div', {'class': 'ingredients'})
    ingreds = [cleanIngred(s.getText()) for s in ingreds.findAll('li')]

    fname = 'PorkRecipe.txt'
    with open(fname, 'w') as outf:
        outf.write('\n'.join(ingreds))

if __name__=="__main__":
    main()

结果是

olive oil
chicken broth
garlic,
paprika
garlic powder
poultry seasoning
dried oregano
dried basil
thick cut boneless pork chops
salt and pepper to taste

?我不知道为什么它在其中留下了逗号-s.strip(string.punctuation)应该已经解决了。



 类似资料:
  • 我一直在使用下面的代码来解析链接中的网页https://www.blogforacure.com/members.php.代码将返回给定页面的所有成员的链接。 但是我只得到上面页面的前10个链接。即使在打印美化选项时,我也只能看到前10个链接。

  • 我使用beautifulsoup查找网页上的页数,但在编写代码时: 它给出了以下错误: 回溯(最近一次调用):文件“C:/Users/HangaarLab/Desktop/sonartik/sonartik.py”,第13行,在soup=BeautifulSoup(response.text)TypeError中:“模块”对象不可调用 在另一台计算机中,代码运行,但它给出了以下警告: UserWa

  • 我有一小段代码来从web站点中提取表数据,然后以csv格式显示。问题是for循环多次打印记录。我不确定是不是因为 标签。顺便说一句,我是Python新手。谢谢你的帮助!

  • 我已经获得了刮取第一页的代码,但是url从: https://www.expansion.com/empresas-de/ganaderia/granjas-en-general/index.html -- 如何创建从第2页到第65页的循环?非常感谢!

  • 我试图刮此页上Flipkart: http://www.flipkart.com/moto-x-play/p/itmeajtqp9sfxgsk?pid=MOBEAJTQRH4CCRYM 我试图找到的div类"fk-ui-ccarousel超级容器相同的vreco部分reco-carousel-边界-顶部sameHorizontalReco",但它返回空结果。 divs是空的。我使用inspect元

  • 问题内容: 我正在尝试在Python 2.7中安装BeautifulSoup 。我不断收到错误消息,无法理解原因。 我按照说明安装了pip,该pip已安装到以下目录:,然后尝试将其添加到路径中并运行命令。 尝试了两种不同的方法: 都给我这个错误信息: 该外壳突出显示“安装”一词,并说这是无效的语法。 我不知道发生了什么,所以任何帮助将不胜感激。 问题答案: 是 命令行工具 ,而不是Python语法

  • 我从谷歌应用商店抓取应用名称,每个网址作为输入,我只得到60个应用(因为如果用户不向下滚动,网站会呈现60个应用)。它是如何工作的,我如何才能从一个页面刮所有的应用程序使用美丽的汤和/或硒? 非常感谢。 这是我的密码:

  • 我有一个带有div标签的页面源,如下面的示例页面源。我想像下面的例子一样刮掉所有的网址,并将它们保存在列表中。 示例url: 来自: 我尝试使用下面的代码从href中刮取网址。我试图使用span类来过滤只包含作业卡search__easy飞机的div标签。代码不返回任何网址,只是一个空列表。我对美丽的汤和硒不熟悉。如果有人能指出我的问题是什么,并提出一个解决方案,我会很高兴。特别是如果你也能给出一