【问题标题】:python string find function doesn't give position from text returned by beautifulsouppython字符串查找函数不从beautifulsoup返回的文本中给出位置
【发布时间】:2017-12-03 00:23:27
【问题描述】:

我正在尝试抓取 10-K 文件的一部分。我在识别“第 7(a) 项”的位置时遇到问题。来自 beautifulsoup 返回的文本,尽管其中包含单词。但是以下代码适用于我制作的包含“项目 7(a)”的字符串。

import urllib2
import re
import bs4 as bs
url=https://www.sec.gov/Archives/edgar/data/1580608/000158060817000015/santander201610-k.htm'

html = urllib2.urlopen(url).read().decode('utf8')
soup = bs.BeautifulSoup(html,'lxml')
text = soup.get_text()
text = text.encode('utf-8')
text = text.lower()
print type(text)
print len(text)
text1 = "hf dfbd item 7. abcd sfjsdf sdbfjkds item 7(a). adfbdf item 8. skjfbdk item 7. sdfkba ootgf sffdfd item 7(a). sfbdskf sfdf item 8. sdfbksdf "
print text.find('item 7(a)')
print text1.find('item 7(a)')

Output:
<type 'str'>
592214
-1
37

【问题讨论】:

  • 你在使用python2吗?
  • 是的。我正在使用 Python 2.7。我也在 Python 3.6 中尝试过,但得到了相同的结果。
  • 你显示text了吗?也许服务器会为您提供与 Web 浏览器不同的结果。
  • 我打印了文本。字符串“项目 7(a)”。在里面。
  • 我进行了一些测试。您的代码在这里没有问题。粘贴您的“文本”的快照可能吗?它实际存在的索引是什么?

标签: python string beautifulsoup find


【解决方案1】:

页面使用实体&amp;nbsp;Not Breaking SPace)(带有字符代码160
而不是文本ITEM 7(A)中的普通空格(代码32

您可以将代码160 (chr(160)) 的所有字符替换为普通空格 (" ")。
在 Python 2(编码后)中,您必须替换两个字符 - 194160

text = text.replace(chr(160), " ") # Python 3
text = text.replace(char(194)+chr(160), " ") # Python 2

完整示例

#import urllib.request as urllib2 # Python 3
import urllib2
import re
import bs4 as bs

url='https://www.sec.gov/Archives/edgar/data/1580608/000158060817000015/santander201610-k.htm'

html = urllib2.urlopen(url).read().decode('utf8')
soup = bs.BeautifulSoup(html,'lxml')
text = soup.get_text()
text = text.encode('utf-8') # only Python 2
text = text.lower()

#text = text.replace(chr(160), " ") # Python 3
text = text.replace(char(194)+chr(160), " ") # Python 2

search = 'item 7(a)'

# find every occurence in text    
pos = 0
while True:
    pos = text.find(search, pos)
    if pos == -1:
        break
    #print(pos, ">"+text[pos-1]+"<", ord(text[pos-1]))
    print(text[pos:pos+20])
    pos += 1

编辑:仅使用 Python 3 测试过

您可以在取消转义后搜索字符串'item&amp;nbsp;7(a)'
但是你必须知道在这个地方你必须使用&amp;nbsp; 而不是" "

from html import unescape

search = unescape('item&nbsp;7(a)')

完整代码

#import urllib.request as urllib2 # Python 3
import urllib2
import re
import bs4 as bs

url='https://www.sec.gov/Archives/edgar/data/1580608/000158060817000015/santander201610-k.htm'

html = urllib2.urlopen(url).read().decode('utf8')
soup = bs.BeautifulSoup(html,'lxml')
text = soup.get_text()
text = text.lower()

from html import unescape

search = unescape('item&nbsp;7(a)')

# find every occurence in text    
pos = 0
while True:
    pos = text.find(search, pos)
    if pos == -1:
        break
    #print(pos, ">"+text[pos-1]+"<", ord(text[pos-1]))
    print(text[pos:pos+20])
    pos += 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-22
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    相关资源
    最近更新 更多