【问题标题】:TypeError: descriptor 'split' requires a 'str' object but received a 'bytes'TypeError:描述符“split”需要一个“str”对象但收到一个“bytes”
【发布时间】:2021-07-29 09:44:04
【问题描述】:

我正在尝试使用 Github 上提供的 python 脚本从 ESPN Cricinfo 抓取数据。代码如下。

import urllib.request as ur
import csv
import sys
import time
import os
import unicodedata
from urllib.parse import urlparse
from bs4 import BeautifulSoup

BASE_URL = 'http://www.espncricinfo.com'
for i in range(0, 6019):
url = 'http://search.espncricinfo.com/ci/content/match/search.html?search=first%20class;all=1;page='
    soupy = BeautifulSoup(ur.urlopen(url + str(i)).read())

    time.sleep(1)
    for new_host in soupy.findAll('a', {'class' : 'srchPlyrNmTxt'}):
        try:
            new_host = new_host['href']
        except:
            continue
        odiurl = BASE_URL + urlparse(new_host).geturl()
        new_host = unicodedata.normalize('NFKD', new_host).encode('ascii','ignore')
        print (new_host)
        print (str.split(new_host, "/"))[4]
        html = urllib2.urlopen(odiurl).read()
        if html:
            with open('espncricinfo-fc/{0!s}'.format(str.split(new_host, "/")[4]), "wb") as f:
                f.write(html)

错误就在这一行。

print (str.split(new_host, "/"))[4]

TypeError: 描述符“split”需要一个“str”对象但收到一个“bytes” 您的任何帮助将不胜感激。谢谢

【问题讨论】:

  • urllib2 在 py3 标准库中不存在,你确定这是 py3 吗?

标签: python csv beautifulsoup scrape


【解决方案1】:

使用

str.split(new_host.decode("utf-8"), "/")[4]

.decode("utf-8") 显然是最重要的部分。这会将您的 byte 对象转换为字符串。

另一方面,请注意urllib2(顺便说一句,您正在使用但未导入)不再使用(请参阅this)。相反,您可以使用from urllib.request import urlopen

编辑:这是完整的代码,不会给您您在问题中描述的错误。我要强调的是,因为没有之前创建的文件,with open(...) 语句会给你一个FileNotFoundError

import urllib.request as ur
import csv
import sys
import time
import os
import unicodedata
from urllib.parse import urlparse
from bs4 import BeautifulSoup
from urllib.request import urlopen

BASE_URL = 'http://www.espncricinfo.com'
for i in range(0, 6019):
    url = 'http://search.espncricinfo.com/ci/content/match/search.html?search=first%20class;all=1;page='
    soupy = BeautifulSoup(ur.urlopen(url + str(i)).read())

    time.sleep(1)
    for new_host in soupy.findAll('a', {'class' : 'srchPlyrNmTxt'}):
        try:
            new_host = new_host['href']
        except:
            continue
        odiurl = BASE_URL + urlparse(new_host).geturl()
        new_host = unicodedata.normalize('NFKD', new_host).encode('ascii','ignore')
        print(new_host)
        print(str.split(new_host.decode("utf-8"), "/")[4])
        html = urlopen(odiurl).read()
        if html:
            with open('espncricinfo-fc/{0!s}'.format(str.split(new_host.decode("utf-8"), "/")[4]), "wb") as f:
                f.write(html)

【讨论】:

  • 我尝试了建议的修改,但错误仍然存​​在于这一行中。现在它说无效的语法。 @Camilo Martinez
  • 我把print 语句改成了这个,修正了错误的括号,执行没有问题。您还需要在 with open(...) 中进行更改。
  • 你能写出我需要修改的完整代码行吗?提前致谢
  • 我用完整的代码编辑了我的答案。请注意免责声明。
猜你喜欢
  • 1970-01-01
  • 2018-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多