我的python(ver 2.7)脚本运行良好,从本地html文件中获取一些公司名称,但是当涉及到某个特定的国家/地区名称时,它会出现此错误"UnicodeEncodeError:'ascii'compodec无法编码字符"
当这个公司名称出现时特别收到错误
公司名称:KühlfixKälteanlagenIng.GerhardDoczekal&Co.KG
该链接无法处理
Traceback (most recent call last):
File "C:\Python27\Process2.py", line 261, in <module>
flog.write("\nCompany Name: "+str(pCompanyName))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 9: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
错误在这行代码中给出:
if companyAlreadyKnown == 0:
for hit in soup2.findAll("h1"):
print "Company Name: "+hit.text
pCompanyName = hit.text
flog.write("\nCompany Name: "+str(pCompanyName))
companyObj.setCompanyName(pCompanyName)
Run Code Online (Sandbox Code Playgroud) 我正在尝试获取包含以下文本模式的HTML文档中的元素:#\ S {11}
<h2> this is cool #12345678901 </h2>
Run Code Online (Sandbox Code Playgroud)
所以,之前的匹配将使用:
soup('h2',text=re.compile(r' #\S{11}'))
Run Code Online (Sandbox Code Playgroud)
结果将是这样的:
[u'blahblah #223409823523', u'thisisinteresting #293845023984']
Run Code Online (Sandbox Code Playgroud)
我能够得到匹配的所有文本(见上面的行).但我希望文本的父元素匹配,因此我可以将其用作遍历文档树的起点.在这种情况下,我希望返回所有h2元素,而不是文本匹配.
想法?
我想获取<script>
文档中的所有标记,然后根据某些属性的存在(或不存在)处理每个标记.
例如,对于每个<script>
标签,如果属性for
存在,则执行某些操作; 否则,如果属性bar
存在,则执行其他操作.
这是我目前正在做的事情:
outputDoc = BeautifulSoup(''.join(output))
scriptTags = outputDoc.findAll('script', attrs = {'for' : True})
Run Code Online (Sandbox Code Playgroud)
但是这样我<script>
用for
属性过滤所有标签......但是我丢失了其他标签(那些没有for
属性的标签).
当我更新我的包时,我遇到了这个新错误:
class TreeBuilderForHtml5lib(html5lib.treebuilders._base.TreeBuilder):
AttributeError: 'module' object has no attribute '_base'
Run Code Online (Sandbox Code Playgroud)
我尝试更新beautifulsoup
,没有更多的结果.我该如何解决这个问题?
如何从BeautifulSoup中找到的元素中删除所有标签?
据我所知,Python中的两个主要HTML解析库是lxml和BeautifulSoup.我选择了BeautifulSoup作为我正在研究的项目,但除了找到更容易学习和理解的语法之外,我选择了它.但是我看到很多人似乎都喜欢lxml而且我听说lxml更快.
所以我想知道一个优于另一个的优点是什么?我什么时候想使用lxml?什么时候最好使用BeautifulSoup?还有其他值得考虑的图书馆吗?
我想只从我汤中最顶层的元素中提取文字; 但是,soup.text也提供了所有子元素的文本:
我有
import BeautifulSoup
soup=BeautifulSoup.BeautifulSoup('<html>yes<b>no</b></html>')
print soup.text
Run Code Online (Sandbox Code Playgroud)
输出到此是yesno
.我只想'是'.
实现这一目标的最佳方法是什么?
编辑:我也想yes
在解析' <html><b>no</b>yes</html>
' 时输出.
我有以下bs4对象列表:
>>> listing
<div class="listingHeader">
<h2>
....
>>> type(listing)
<class 'bs4.element.Tag'>
Run Code Online (Sandbox Code Playgroud)
我想将原始html提取为字符串.我试过了:
>>> a = listing.contents
>>> type(a)
<type 'list'>
Run Code Online (Sandbox Code Playgroud)
所以这不起作用.我怎样才能做到这一点?
目前我的代码执行如下操作:
soup = BeautifulSoup(value)
for tag in soup.findAll(True):
if tag.name not in VALID_TAGS:
tag.extract()
soup.renderContents()
Run Code Online (Sandbox Code Playgroud)
除了我不想丢弃无效标签内的内容.如何在删除标签但在调用soup.renderContents()时保留内容?
如果我想先刮一个需要用密码登录的网站,怎样才能开始使用beautifulsoup4库用python抓它?以下是我对不需要登录的网站所做的工作.
from bs4 import BeautifulSoup
import urllib2
url = urllib2.urlopen("http://www.python.org")
content = url.read()
soup = BeautifulSoup(content)
Run Code Online (Sandbox Code Playgroud)
如何更改代码以适应登录?假设我要抓的网站是一个需要登录的论坛.一个例子是http://forum.arduino.cc/index.php
beautifulsoup ×10
python ×9
html-parsing ×2
ascii ×1
encoding ×1
html ×1
html5lib ×1
lxml ×1
parsing ×1
regex ×1
text ×1
unicode ×1
web-scraping ×1