- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一段代码来解析网页。我想删除 div、ahref、h1 之间的所有内容。
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
url = "http://en.wikipedia.org/wiki/Viscosity"
try:
ourUrl = opener.open(url).read()
except Exception,err:
pass
soup = BeautifulSoup(ourUrl)
dem = soup.findAll('p')
for i in dem:
print i.text
我想打印 h1、ahref 之间没有任何内容的文本,就像我上面提到的那样。
最佳答案
编辑:来自评论“我想返回不在任何<div>
和</div>
标签之间的文本。”。这应该删除父级具有 div 标签的所有 block :
raw = '''
<html>
Text <div> Avoid this </div>
<p> Nested <div> Don't get me either </div> </p>
</html>
'''
def check_for_div_parent(mark):
mark = mark.parent
if 'div' == mark.name:
return True
if 'html' == mark.name:
return False
return check_for_div_parent(mark)
soup = bs4.BeautifulSoup(raw)
for text in soup.findAll(text=True):
if not check_for_div_parent(text):
print text.strip()
这只会产生两个标签,忽略 div 标签:
Text
Nested
原始回复
目前尚不清楚您到底想做什么。首先,您应该尝试发布一个完整的工作示例,因为您似乎缺少标题。其次,维基百科似乎对“机器人”或自动下载程序持反对态度
Python's `urllib2`: Why do I get error 403 when I `urlopen` a Wikipedia page?
可以通过以下代码行来避免这种情况
import urllib2, bs4
url = r"http://en.wikipedia.org/wiki/Viscosity"
req = urllib2.Request(url, headers={'User-Agent' : "Magic Browser"})
con = urllib2.urlopen( req )
现在我们有了页面,我认为您只想使用 bs4
提取正文。我会做这样的事情
soup = bs4.BeautifulSoup(con.read())
start_pos = soup.find('h1').parent
for p in start_pos.findAll('p'):
para = ''.join([text for text in p.findAll(text=True)])
print para
这给我的文本看起来像:
The viscosity of a fluid is a measure of its resistance to gradual deformation by shear stress or tensile stress. For liquids, it corresponds to the informal notion of "thickness". For example, honey has a higher viscosity than water.[1] Viscosity is due to friction between neighboring parcels of the fluid that are moving at different velocities. When fluid is forced through a tube, the fluid generally moves faster near the axis and very slowly near the walls, therefore some stress (such as a pressure difference between the two ends of the tube) is needed to overcome the friction between layers and keep the fluid moving. For the same velocity pattern, the stress required is proportional to the fluid's viscosity. A liquid's viscosity depends on the size and shape of its particles and the attractions between the particles.[citation needed]
关于python - 删除 <div> 和 <ahref> 之间的内容 Beautiful Soup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18445389/
这里是新手问题。我的所有 li 中都有一个带填充的导航栏,我希望在单击填充时能够定位 ahref。这将通过每个 li 的 onclick/javascript 完成吗?还是有简单的 CSS 方法? 我
在一个网页上我有几个指向项目的链接,例如: View item 1 View item 2 View item 3 出于 SEO 的原因,我想要一个普通的 ahref,但是当有人点击链接时,我想在数据
我在我的 iPhone 应用程序中使用了一些 HTML。我对 HTML 不太了解。 当我们触摸任何超链接(HTML 中的 ahref)时,包含 ahref 的“li”中会出现蓝色选择颜色。我们如何禁用
因此,我在后端发出 api 请求时收到此字符串格式(这是来自 flash 的遗留代码,我们必须将其转换为 html): yahoo google 在客户端,我被要求删除除 ahref 标签之外的 h
我正在尝试向从 ahref html 标记获取的链接添加属性,请有人指导我,我在这方面做错了什么? 链接将从 ahref 标签中获取,并从下拉列表中获取语言和货币,然后最终链接将是"link+"ind
我目前正在使用教程 ( http://www.java-programming.info/tutorial/pdf/csajsp2/07-Cookies.pdf ) 来尝试启用 cookie 创建,我
我已经在我的网页底部实现了这个脚本,以便当有人点击导航中的链接导致页面的某个 anchor 部分时获得那种漂亮的缓慢动画效果。 $('a').click(function(){ $('html, b
我有一个div如下: title 我想要做的是,而不是在 slide_items 中的每个元素中都包含 URL分区
我在所有 $rows 旁边列出了一个删除链接,当我将鼠标悬停在它们上面时,它们会反射(reflect)正确的删除 ID,但是,当我单击 DELETE 时,我会被重定向到 phpfile.php?id=
我正在尝试向 Arraylist 添加 href,这很好地添加到 Arraylist,但链接已损坏。 URL 中问号 (?) 后面的所有内容均不包含在链接中。 有什么我遗漏的吗,代码如下: priva
我正在使用使用 JSON api 端点的 react 创建一个 spfx 小部件。然后它会吐出最新的几篇帖子。 添加 {url} 时变量为 - url 是一个随机的本地主机,而不是完整的 url
这是我的分页代码,在页面底部,显示页码但 ahref 链接不起作用。谁能告诉我哪里出错了。我希望你不需要 css 文件。 另请注意,分页链接(ahref 链接)是第一次创建,但当我从一个页面移动到另一
我有一个像这样的字符串: I like this video: http://www.youtube.com/watch?v=o03pXLuPl6A&hd=1 我想使用 jQuery 删除与 YouT
我正在编写一个程序,在该程序中用户可以将 .wav 文件拖/放到播放列表容器中。然后他们可以按照播放列表容器中的顺序播放所有这些文件。现在我正在制作保存功能,将所有这些 .wav 文件组合成一个文件并
我正在尝试使用一些普通的 Javascript 替换整个页面上的 url 值。我不能使用任何库/框架。到目前为止,这是我在标签之间放置在每个页面顶部的内容: function change_url(
我在使用 DOM 上的 ahref 的 event.preventDefault(); 时遇到问题。 如何防止 URL 发布 nofollow 删除(如使用 JQuery 的 HTML 指定的那样)?
如何像在 textview 中一样显示 HTML 标签? 例如, Hello good morning USER1 and USER2 最佳答案 NSString *htmlString = @"He
在以下网站上:http://www.strategix.co.za/在该页面上,您将看到带有 8 个悬停框的标题“我们的解决方案”。 我想做的是在每个框周围包裹一个 href,这样不仅当您将鼠标悬停在
我有一段代码来解析网页。我想删除 div、ahref、h1 之间的所有内容。 opener = urllib2.build_opener() opener.addheaders = [('User-a
如何翻译 shrinkroute README 文件中的示例: // or views... ">User profile ">User profile 用于 Jade 模板?例如,像 a(href=
我是一名优秀的程序员,十分优秀!