TypeError:“function”类型的对象在使用BeautifulSoup和FancyURLopener时没有len()

2024-04-29 05:12:25 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在尝试使用BeautifulSoup时遇到了一个奇怪的错误/回溯。如果你还记得的话,在我之前的问题中,我和BioPython有过麻烦。虽然这些麻烦或多或少都在解决的边缘,但我有一个新问题。从PMC中刮取的参考并不总是与植物病害对相关。例如,植物或疾病可能发生在参考文献中,而不是全文正文中,从而导致误报。为了解决这个问题,与我们一起工作的另一位实习生建议我使用BeautifulSoup解析PMC页面中的HTML,并检查文本“References”之后是否有植物/疾病发生。在尝试这样做时,我得到了403禁止错误,并从StackOverflow和GitHub上的其他答案推断出NCBI以某种方式阻止了urllib。为了解决这个问题,建议的解决方案是使用Mozilla的FancyURLopener作为中介。然而,我不断地得到这种奇怪的回溯,我不能,以我的生命来说,找出代码的错误。以下是回溯:

scraperscript_python.py:54: DeprecationWarning: AppURLopener style of invoking requests is deprecated. Use newer urlopen functions/methods
  opener = AppURLopener()
Traceback (most recent call last):
  File "scraperscript_python.py", line 58, in <module>
    pmc_refsoup = soup(page_html, "html.parser")
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python37\lib\site-packages\bs4\__init__.py", line 275, in __init__
    elif len(markup) <= 256 and (
TypeError: object of type 'function' has no len()

以下是通向第58行(包括第58行)的线路:

# First print statement
  for plant, disease in plant_disease_list:
    search_query = generate_search_query(plant, disease)
    handle1 = Entrez.esearch(db="pmc", term=search_query, retmax="10")
    record1 = Entrez.read(handle1)
    pubmed_ids = record1.get("IdList")
    if len(pubmed_ids)==0:
      print("{}, {}, None".format(plant, disease))
  # Else statement, initializing BeautifulSoup for parsing fulltext to avoid false positives
    else:
      for pubmed_id in pubmed_ids:
        handle2 = Entrez.esummary(db="pmc", id=pubmed_id)
        records = Entrez.read(handle2)
        pmc_main = pubmed_id
        pmcid_string = str("http://ncbi.nlm.nih.gov/pmc/articles/PMC")
        append_pmcid = ("").join((pmcid_string + pmc_main))
        my_url = append_pmcid
        class AppURLopener(urllib.request.FancyURLopener):
          version = "Mozilla/5.0"
        opener = AppURLopener()
        uClient_response = opener.open(my_url)
        page_html = uClient_response.read
        uClient_response.close()
        pmc_refsoup = soup(page_html, "html.parser")

我觉得有一些明显的东西我错过了,但我不能找出它,这让我发疯


Tags: inpyidhtml错误entrezopenerplant
1条回答
网友
1楼 · 发布于 2024-04-29 05:12:25

排队

page_html = uClient_response.read

您缺少调用read方法的括号,这意味着您将该方法本身分配给page_html,然后将其作为参数传递给soup,从而导致TypeError

调用read的正确方法是:

page_html = uClient_response.read()

相关问题 更多 >