python3,判断字符串是否相等,学习过程中,我用了四种做法都错,求指点。。。

2018-08-19 15:47:21 +08:00
 pppguest3962
htmlfile = open('D:/test.htm','rb')
htmlhandle = htmlfile.read()
soup = BeautifulSoup(htmlhandle, 'lxml')
table = soup.select('tr > td')

for tr in table:
    infostr = re.findall('stream="adfs-(\d+)',tr.prettify())
    #做法 1
    if (infostr.__str__() in "1776"):
        print(infostr.__str__())
        
    #做法 2
    import operator       
    if (operator.eq(infostr.__str__(),"1776"))
        print(infostr.__str__())
    
    #做法 3,这个应该是 python2 的时候能用起来,python3 应该不行了吧?
    if (infostr.__str__() == "1776")
        print(infostr.__str__())
    
    #做法 4,把 1776 强制指定为字符串变量,结果还是不行
    import operator       
    if (operator.eq(infostr.__str__(),"1776"))
        print(infostr.__str__())

想通过正则,判断截取出来的字符,是不是文本 1776 这个字符串?

已试过:

for tr in table:
   print tr

确定 tr 是已经获取到字符串的了。。。。

求问正确的做法应该是?

5023 次点击
所在节点    Python
5 条回复
kba977
2018-08-19 15:50:44 +08:00
infostr 是一个列表呀
ranleng
2018-08-19 15:58:28 +08:00
如 #1 说的.
re.findall 返回的是一个 list
pppguest3962
2018-08-19 15:58:44 +08:00
@kba977,我真是傻了。。。意识到自己的错误,不胜感谢!!!
wwqgtxx
2018-08-19 21:01:29 +08:00
而且为啥要手动调用.__str__不应该用 str()的么
talen666
2018-08-19 22:37:41 +08:00
if infostr[0].find('1776) != -1

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/481172

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX