我正在尝试使用特定HTML文件的以下代码
from BeautifulSoup import BeautifulSoup
import re
import codecs
import sys
f = open('test1.html')
html = f.read()
soup = BeautifulSoup(html)
body = soup.body.contents
para = soup.findAll('p')
print str(para).encode('utf-8')
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 9: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我该如何调试?
当我删除对print函数的调用时,我没有收到任何错误.
我有这个HTML:
<input type="text" class="txtSearch">
<input type="submit" value="Search" class="sbtSearch">
Run Code Online (Sandbox Code Playgroud)
我需要的是在文本字段中写入,然后单击使用python提交.输入标记不在Form中.我怎么能这样做?
我有两种情况,我想用自定义html属性刮取html这是html的例子.如何使用自定义属性"限制"清除所有元素.
<div class="names" limit="10">Bar</div>
<div id="30" limit="20">Foo</div>
<li limit="x">Baz</li>
Run Code Online (Sandbox Code Playgroud)
第二种情况类似,但所有相同的html标签
<div class="names" limit="10">Bar</div>
<div class="names" limit="20">Bar</div>
<div class="names" limit="30">Bar</div>
Run Code Online (Sandbox Code Playgroud)
我的问题与如何找到只有某些属性的标签不同- BeautifulSoup,因为后者使用特定标签定位属性值,而我的问题仅在标记或值的情况下查找目标属性
我有一个Python脚本,可以在html页面中删除元素的src属性<video>.使用此页面视频上的浏览器检查器,我可以看到我需要抓取的视频元素,但直接查看页面源只显示ember应用程序JavaScript文件.
我需要做什么来访问保存<video>元素的"内部框架"标记,以便我可以抓取src属性?
编辑所以它不是那么广泛
我正在尝试从HTML代码中获取所有href,并将其存储在列表中以供将来处理,例如:
示例网址:www.example-page-xl.com
<body>
<section>
<a href="/helloworld/index.php"> Hello World </a>
</section>
</body>
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码列出href的:
import bs4 as bs4
import urllib.request
sauce = urllib.request.urlopen('https:www.example-page-xl.com').read()
soup = bs.BeautifulSoup(sauce,'lxml')
section = soup.section
for url in section.find_all('a'):
print(url.get('href'))
Run Code Online (Sandbox Code Playgroud)
但是我想将URL存储为:www.example-page-xl.com/helloworld/index.php而不仅仅是/helloworld/index.php的相对路径
不需要使用相对路径追加/加入URL,因为当我加入URL和相对路径时,动态链接可能会有所不同.
简而言之,我想刮掉绝对URL而不是单独的相对路径(并且没有加入)
我有 bs4.element.Tag 类型的元素
<a class="nav-link match-link-stats" href="/football/matches/match867851_Kalteng_Putra-Arema-online/" title="Stat"><i class="icon-match-link"></i></a>
Run Code Online (Sandbox Code Playgroud)
我想从这个元素中得到“/football/matches/match867851_Kalteng_Putra-Arema-online/”。怎么做?
美丽的汤文档提供了属性.contents和.children来访问给定标记的子元素(分别是列表和迭代),并包括Navigable Strings和Tags.我只想要Tag类型的孩子.
我目前正在使用列表理解来完成此任务:
rows=[x for x in table.tbody.children if type(x)==bs4.element.Tag]
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有一个更好/更pythonic /内置的方式来获得Tag儿童.
我正在尝试使用BeautifulSoup Python库解析HTML文档,但结构会被<br>标记扭曲.让我举个例子.
输入HTML:
<div>
some text <br>
<span> some more text </span> <br>
<span> and more text </span>
</div>
Run Code Online (Sandbox Code Playgroud)
BeautifulSoup解释的HTML:
<div>
some text
<br>
<span> some more text </span>
<br>
<span> and more text </span>
</br>
</br>
</div>
Run Code Online (Sandbox Code Playgroud)
在源头,跨度可以被认为是兄弟姐妹.在解析之后(使用默认解析器),跨度突然不再是兄弟,因为br标签成为结构的一部分.
我可以想到解决这个问题的解决方案是<br>在将html注入Beautifulsoup之前完全剥离标签,但这似乎并不优雅,因为它需要我更改输入.有什么更好的方法来解决这个问题?
我想做这样的事情:
soup.find_all('td', attrs!={"class":"foo"})
Run Code Online (Sandbox Code Playgroud)
我想找到所有没有foo类的td.
显然以上不起作用,有什么作用?
我有一个页面,我需要获取与BS4一起使用的源,但页面中间需要1秒(可能更少)来加载内容,并且requests.get在加载部分之前捕获页面的源,如何在获取数据之前,我可以等一下吗?
r = requests.get(URL + self.search, headers=USER_AGENT, timeout=5 )
soup = BeautifulSoup(r.content, 'html.parser')
a = soup.find_all('section', 'wrapper')
Run Code Online (Sandbox Code Playgroud)
<section class="wrapper" id="resultado_busca">
Run Code Online (Sandbox Code Playgroud) beautifulsoup ×10
python ×9
html ×2
python-2.7 ×2
html-parsing ×1
mechanize ×1
python-3.x ×1
unicode ×1
web-scraping ×1