Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 美团:<;部门级<;跨度等级></span>&书信电报;跨度等级>;我想要的文本</span>;_Python - Fatal编程技术网

Python 美团:<;部门级<;跨度等级></span>&书信电报;跨度等级>;我想要的文本</span>;

Python 美团:<;部门级<;跨度等级></span>&书信电报;跨度等级>;我想要的文本</span>;,python,Python,我试图使用BeautifulSoup提取由span括起来的字符串,其id=“titleDescription” <div class="itemText"> <div class="wrapper"> <span class="itemPromo">Customer Choice Award Winner</span> <a href="http://www.newegg.com/Product/Pro

我试图使用BeautifulSoup提取由span括起来的字符串,其id=“titleDescription”

<div class="itemText">
    <div class="wrapper">
        <span class="itemPromo">Customer Choice Award Winner</span>
        <a href="http://www.newegg.com/Product/Product.aspx?Item=N82E16819116501" title="View Details" >
            <span class="itemDescription" id="titleDescriptionID" style="display:inline">Intel Core i7-3770K Ivy Bridge 3.5GHz &#40;3.9GHz Turbo&#41; LGA 1155 77W Quad-Core Desktop Processor Intel HD Graphics 4000 BX80637I73770K</span>
            <span class="itemDescription" id="lineDescriptionID" style="display:none">Intel Core i7-3770K Ivy Bridge 3.5GHz &#40;3.9GHz Turbo&#41; LGA 1155 77W Quad-Core Desktop Processor Intel HD Graphics 4000 BX80637I73770K</span>
        </a>
    </div>

客户选择奖得主
代码片段

f = open('egg.data', 'rb')
content = f.read()
content = content.decode('utf-8', 'replace')
content = ''.join([x for x in content if ord(x) < 128])

soup = bs(content)

for itemText in soup.find_all('div', attrs={'class':'itemText'}):
    wrapper = itemText.div
    wrapper_href = wrapper.a
    for child in wrapper_href.descendants:
        if child['id'] == 'titleDescriptionID':
           print(child, "\n")
f=open('egg.data','rb')
content=f.read()
content=content.decode('utf-8','replace')
content=''.join([x代表内容中的x,如果ord(x)<128])
汤=bs(含量)
对于汤中的itemText.find_all('div',attrs={'class':'itemText'}):
包装器=itemText.div
包装器\u href=wrapper.a
对于wrapper_href.substands中的子项:
如果子项['id']='titleDescriptionID':
打印(子项“\n”)
回溯错误:

Traceback (most recent call last):
  File "egg.py", line 66, in <module>
    if child['id'] == 'titleDescriptionID':
TypeError: string indices must be integers
回溯(最近一次呼叫最后一次):
文件“egg.py”,第66行,在
如果子项['id']='titleDescriptionID':
TypeError:字符串索引必须是整数

在您的代码中,
wrapper_href.subjections
至少包含4个元素、2个span标记和2个由2个span标记包围的字符串。它递归地搜索它的子项。

wrapper\u href.子项
包括任何子项,这正是您所忽略的
NavigableString
本质上是字符串对象,您正试图使用
child['id']
行对其进行索引:

>>> next(wrapper_href.descendants)
u'\n'
为什么不直接使用
itemText.find('span',id='titleDescriptionID')
加载标记呢

演示:

>>查找汤中的itemText.find_all('div',attrs={'class':'itemText'}):
...     打印itemText.find('span',id='titleDescriptionID')
...     打印itemText.find('span',id='titleDescriptionID').text
... 
英特尔酷睿i7-3770K常春藤桥3.5GHz(3.9GHz Turbo)LGA 1155 77W四核桌面处理器英特尔高清图形4000 BX80637I73770K
英特尔酷睿i7-3770K常春藤桥3.5GHz(3.9GHz Turbo)LGA 1155 77W四核桌面处理器英特尔高清图形4000 BX80637I73770K

当我们使用BeautifulSoup搜索标记时,我们会得到一个BeautifulSoup.tag对象,它可以直接用来访问它的其他属性,如内部内容、样式、href等。

(对于整个文件)(整个python脚本)粘贴不再加载BTW这是我选择我的方法的原因:我需要解析多个这样的HTML块。我已将完整的原始文件上载到pastebin。我之前没有这样做,因为bpaste.net将其视为垃圾邮件,验证码不起作用(内部服务器错误)。无论如何,使用您的方法可以很好地工作,但由于某些原因,它不会解析该文件中的所有“span”+attrs标记-只是它找到的第一个。它应该只找到第一个,因为我选择了属性id为的span。id是唯一的。如果要选择更多跨距,请选择正确的属性或编号。@paleyviener\n 30180如何获得30180值?我更喜欢这个答案,它似乎适合我的问题。非常感谢。
>>> next(wrapper_href.descendants)
u'\n'
>>> for itemText in soup.find_all('div', attrs={'class':'itemText'}):
...     print itemText.find('span', id='titleDescriptionID')
...     print itemText.find('span', id='titleDescriptionID').text
... 
<span class="itemDescription" id="titleDescriptionID" style="display:inline">Intel Core i7-3770K Ivy Bridge 3.5GHz (3.9GHz Turbo) LGA 1155 77W Quad-Core Desktop Processor Intel HD Graphics 4000 BX80637I73770K</span>
Intel Core i7-3770K Ivy Bridge 3.5GHz (3.9GHz Turbo) LGA 1155 77W Quad-Core Desktop Processor Intel HD Graphics 4000 BX80637I73770K
from BeautifulSoup import BeautifulSoup
pool = BeautifulSoup(html) # where html contains the whole html as string

for item in pool.findAll('span', attrs={'id' : 'titleDescriptionID'}):
    print item.string