我在处理从不同网页(在不同网站上)获取的文本中的unicode字符时遇到问题.我正在使用BeautifulSoup.
问题是错误并不总是可重现的; 它有时适用于某些页面,有时候,它会通过抛出一个UnicodeEncodeError
.我已经尝试了几乎所有我能想到的东西,但是我没有找到任何可以持续工作的东西而不会抛出某种与Unicode相关的错误.
导致问题的代码部分之一如下所示:
agent_telno = agent.find('div', 'agent_contact_number')
agent_telno = '' if agent_telno is None else agent_telno.contents[0]
p.agent_info = str(agent_contact + ' ' + agent_telno).strip()
Run Code Online (Sandbox Code Playgroud)
以下是运行上述代码段时在SOME字符串上生成的堆栈跟踪:
Traceback (most recent call last):
File "foobar.py", line 792, in <module>
p.agent_info = str(agent_contact + ' ' + agent_telno).strip()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我怀疑这是因为某些页面(或更具体地说,来自某些站点的页面)可能被编码,而其他页面可能是未编码的.所有这些网站都位于英国,并提供供英国消费的数据 - 因此,没有与内部化或处理用英语以外的任何文字处理的文本相关的问题.
有没有人有任何想法如何解决这个问题,以便我可以一致地解决这个问题?
我使用Beautifulsoup解析带有"class"属性的html元素时遇到问题.代码看起来像这样
soup = BeautifulSoup(sdata)
mydivs = soup.findAll('div')
for div in mydivs:
if (div["class"] == "stylelistrow"):
print div
Run Code Online (Sandbox Code Playgroud)
我在脚本完成后"同一行"收到错误.
File "./beautifulcoding.py", line 130, in getlanguage
if (div["class"] == "stylelistrow"):
File "/usr/local/lib/python2.6/dist-packages/BeautifulSoup.py", line 599, in __getitem__
return self._getAttrMap()[key]
KeyError: 'class'
Run Code Online (Sandbox Code Playgroud)
我该怎么摆脱或这个错误?
我有以下汤:
<a href="some_url">next</a>
<span class="class">...</span>
Run Code Online (Sandbox Code Playgroud)
从这里我想提取href, "some_url"
如果我只有一个标签,我可以做到,但这里有两个标签.我也可以得到文字,'next'
但这不是我想要的.
此外,是否有一个很好的描述API的例子.我正在使用标准文档,但我正在寻找更有条理的东西.
...
soup = BeautifulSoup(html, "lxml")
File "/Library/Python/2.7/site-packages/bs4/__init__.py", line 152, in __init__
% ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
Run Code Online (Sandbox Code Playgroud)
以上输出在我的终端上.我在Mac OS 10.7.x上.我有Python 2.7.1,并按照本教程获得Beautiful Soup和lxml,它们都已成功安装并使用位于此处的单独测试文件.在导致此错误的Python脚本中,我包含了这一行:
from pageCrawler import comparePages
在pageCrawler文件中,我包含以下两行:
from bs4 import BeautifulSoup
from urllib2 import urlopen
任何有关确定问题是什么以及如何解决问题的帮助都将不胜感激.
我正在试图抓一个网站,但它给了我一个错误.
我正在使用以下代码:
import urllib.request
from bs4 import BeautifulSoup
get = urllib.request.urlopen("https://www.website.com/")
html = get.read()
soup = BeautifulSoup(html)
print(soup)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
File "C:\Python34\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 70924-70950: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这个问题?
TypeError:需要类似字节的对象,而不是'str'
在执行下面的python代码时将错误的表格数据保存在Csv文件中.不知道如何获得rideup.pls帮助我.
import csv
import requests
from bs4 import BeautifulSoup
url='http://www.mapsofindia.com/districts-india/'
response=requests.get(url)
html=response.content
soup=BeautifulSoup(html,'html.parser')
table=soup.find('table', attrs={'class':'tableizer-table'})
list_of_rows=[]
for row in table.findAll('tr')[1:]:
list_of_cells=[]
for cell in row.findAll('td'):
list_of_cells.append(cell.text)
list_of_rows.append(list_of_cells)
outfile=open('./immates.csv','wb')
writer=csv.writer(outfile)
writer.writerow(["SNo", "States", "Dist", "Population"])
writer.writerows(list_of_rows)
Run Code Online (Sandbox Code Playgroud)
在最后一行上方.
soup.find("tagName", { "id" : "articlebody" })
Run Code Online (Sandbox Code Playgroud)
为什么这不会返回<div id="articlebody"> ... </div>
标签和之间的东西?它什么都不返回.而且我知道它存在的事实因为我正盯着它
soup.prettify()
Run Code Online (Sandbox Code Playgroud)
soup.find("div", { "id" : "articlebody" })
也行不通.
编辑:这篇文章没有答案 - 如何删除?我发现BeautifulSoup没有正确解析,这可能实际上意味着我试图解析的页面没有在SGML或其他任何方式正确格式化.
如何检索网页的链接并使用Python复制链接的URL地址?
我想建立一个网站,显示亚马逊和电子海湾产品价格之间的比较.哪个更好,为什么?我对BeautifulSoup有点熟悉,但与Scrapy爬虫不太相似.
我正在使用Python并使用Flask.当我在我的电脑上运行我的主要的Python文件,它完美的作品,但是当我启动VENV和运行在终端的瓶的Python文件,它说,我的主要的Python文件中有"无模块命名BS4." 非常感谢任何意见或建议.
beautifulsoup ×10
python ×9
web-scraping ×2
csv ×1
flask ×1
html ×1
html-table ×1
hyperlink ×1
importerror ×1
lxml ×1
python-2.7 ×1
python-2.x ×1
python-3.x ×1
scrapy ×1
tags ×1
unicode ×1
urllib ×1
web-crawler ×1