草庐IT

BeautifulSoup4

全部标签

python - replaceWith() 后的 find() 不起作用(使用 BeautifulSoup)

请考虑以下pythonsession:>>>fromBeautifulSoupimportBeautifulSoup>>>s=BeautifulSoup("Thisisatest.");myi=s.find("i")>>>myi.replaceWith(BeautifulSoup("was"))>>>s.find("i")>>>s=BeautifulSoup("Thisisatest.");myi=s.find("i")>>>myi.replaceWith("was")>>>s.find("i")test请注意第4行后缺少s.find("i")的输出!这是什么原因?有解决方法吗?编辑:

python - Urllib2 & BeautifulSoup : Nice couple but too slow - urllib3 & threads?

当我听到有关线程和urllib3的一些好消息时,我正在寻找一种方法来优化我的代码。显然,人们不同意哪种解决方案是最好的。下面我的脚本的问题是执行时间:太慢了!第1步:我获取此页面http://www.cambridgeesol.org/institutions/results.php?region=Afghanistan&type=&BULATS=on第2步:我用BeautifulSoup解析页面第3步:我将数据放入excel文档中第4步:我对我列表(大列表)中的所有国家/地区一次又一次地执行此操作(我只是将url中的“阿富汗”更改为另一个国家)这是我的代码:ws=wb.add_she

Python:BeautifulSoup按其类在div标签之间提取字符串

importurllib,urllib2frombs4importBeautifulSoup,Commenturl='http://www.amazon.in/product-reviews/B00CE2LUKQ/ref=cm_cr_pr_top_link_1?ie=UTF8&showViewpoints=0&sortBy=bySubmissionDateDescending'content=urllib2.urlopen(url).read()soup=BeautifulSoup(content,"html.parser")rows=soup.find_all('div',attrs

python - 如何在 python 的 BeautifulSoup4 中使用 .next_sibling 时忽略空行

因为我想删除html网站中重复的占位符,所以我使用BeautifulSoup的.next_sibling运算符。只要重复项在同一行,就可以正常工作(参见数据)。但有时它们之间有一个空行-所以我希望.next_sibling忽略它们(看看data2)这是代码:frombs4importBeautifulSoup,Tagdata="method-removed-heremethod-removed-heremethod-removed-here"data2="""method-removed-heremethod-removed-heremethod-removed-heremethod-

python - BeautifulSoup `find_all` 生成器

有什么办法可以转find_all变成一个内存效率更高的生成器?例如:给定:soup=BeautifulSoup(content,"html.parser")returnsoup.find_all('item')我想改用:soup=BeautifulSoup(content,"html.parser")whileTrue:yieldsoup.next_item_generator()(假设正确处理最终的StopIteration异常)有一些内置的生成器,但不会在查找中产生下一个结果。find只返回第一项。数以千计的项目,find_all占用了很多内存。对于5792项,我发现RAM刚好超过

python - 没有名为 BeautifulSoup 的模块(但应该安装它)

这个问题在这里已经有了答案:ImportError:NomodulenamedBeautifulSoup(8个答案)关闭2年前。我下载了BeautifulSoup。然后我升级了pip:pipinstall--upgradepip然后,安装BS:pipinstallbeautifulsoup4看起来一切正常,但现在当我运行这三行代码时:fromBeautifulSoupimportBeautifulSoupimporturllib2importcsv我收到这个错误。Traceback(mostrecentcalllast):File"C:\Users\rshuell001.spyder2

python - 如何使用 BeautifulSoup 获取选项文本

我想使用BeautifulSoup获取以下html中的选项文本。例如:我想获得2002/12、2003/12等。2002/122003/122004/122005/122006/122007/122008/122009/122010/122011/12获取内容的最佳方式是什么?现在我正在使用以下代码,但我不知道如何为此使用漂亮的汤。如果html文件中有多个选择区域,结果将不正确。这是我目前所拥有的:importurllib2frombs4importBeautifulSoupimportlxmlsoup=BeautifulSoup(urllib2.urlopen("./test.htm

python - 我怎样才能从 BeautifulSoup 中获取 CData

我有一个正在抓取的网站,其结构如下所示。我希望能够从CDatablock中获取信息。我正在使用BeautifulSoup从页面中提取其他信息,所以如果解决方案可以解决这个问题,它将有助于降低我的学习曲线,因为我是Python新手。具体来说,我想了解隐藏在CData语句中的两种不同类型的数据。第一个只是文本我很确定我可以向它抛出一个正则表达式并得到我需要的东西。对于第二种类型,如果我可以将具有html元素的数据放入它自己的beautifulsoup中,我可以解析它。我只是在学习python和beautifulsoup,所以我正在努力寻找能够单独给我CData的神奇咒语。CowsandSh

loops - 如何使用 BeautifulSoup 成对提取表数据?

我的数据样本:Google07/11/2001CAApple27/08/2001Microsoft01/11/1991Beautifulsoup代码:table=soup.find("table",id="history")rows=table.findAll('tr')fortrinrows:cols=tr.findAll('td')fortdincols:printtd.find(text=True)MySQL存储所需的输出(列表):['Google|07/11/2001|CA','Apple|27/08/2001','Microsoft|01/11/1991']我的输出(很难将正

python - 我怎样才能从 bs4 导入 BeautifulSoup?

这段代码:frombs4importBeautifulSoup不起作用,并给出此错误:raiseAttributeError,"'%s'objecthasnoattribute'%s'"%(self.__class__.__name__,attr)^SyntaxError:invalidsyntax我该怎么办? 最佳答案 你应该使用pip来安装,所以你可以简单地做pipinstallbeautifulsoup4这将安装最新的BS4,截至2013年8月15日为4.3.1。它支持Python3。