Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.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 属性错误:';str';对象没有属性';is#xml';_Python_Parsing_Beautifulsoup - Fatal编程技术网

Python 属性错误:';str';对象没有属性';is#xml';

Python 属性错误:';str';对象没有属性';is#xml';,python,parsing,beautifulsoup,Python,Parsing,Beautifulsoup,你能帮我一下吗,我得到了这个错误,它指的是init文件 import requests import numpy as np from bs4 import BeautifulSoup def get_text(url): r = requests.get(url) text = r.text return text def get_items(text, top_name, class_name): soup = BeautifulSoup(text, 'h

你能帮我一下吗,我得到了这个错误,它指的是init文件

import requests
import numpy as np
from bs4 import BeautifulSoup

def get_text(url):
    r = requests.get(url)
    text = r.text
    return text

def get_items(text, top_name, class_name):
    soup = BeautifulSoup(text, 'html', 'html.parser')
    vac_list = soup.find('div', {'class': top_name})
    items = vac_list.find('h2', {'class': [class_name]})
    dirty_link = []
    for item in items:
        dirty_link.append(str(item.find('a')))
    return dirty_link

def get_links(dirty_list, start, end):
    links = []
    for row in dirty_list:
        if row != 'None':
            i_beg = row.find(start)
            i_end = row.find(end)
            if i_beg != -1 & i_end != -1:
                links.append(row[i_beg:i_end])
    return links

job_name = str(input("Введите интересующую должность:"))

url = "https://qyzmet.kz/vacansii?q="+str(job_name)+"&l=%D0%9D%D1%83%D1%80-%D0%A1%D1%83%D0%BB%D1%82%D0%B0%D0%BD"

top_name = 'jobs'
class_name = 'title'
start = 'vacancies'
end = '/">Отправить резюме'

text = get_text(url)
dirty_link = get_items(text, top_name, class_name)

links = get_links(dirty_link, start, end)
追溯说

  File "C:/Users/Милена/PycharmProjects/pythonProject1/parser.py", line 39, in <module>
    dirty_link = get_items(text, top_name, class_name)
  File "C:/Users/Милена/PycharmProjects/pythonProject1/parser.py", line 11, in get_items
    soup = BeautifulSoup(text, 'html', 'html.parser')
  File "C:\Users\Милена\Desktop\diplom\bot\lib\site-packages\bs4\__init__.py", line 301, in __init__
    self.is_xml = builder.is_xml
AttributeError: 'str' object has no attribute 'is_xml'
文件“C:/Users/Мццаa/PycharmProjects/pythonProject1/parser.py”,第39行,在
脏链接=获取项目(文本、顶部名称、类名称)
get_项目中的第11行文件“C:/Users/Мцаааа/PycharmProjects/pythonProject1/parser.py”
soup=BeautifulSoup(文本'html','html.parser')
文件“C:\Users\Мциаа\Desktop\diplom\bot\lib\site packages\bs4\\ uuuuu init\uuuuuu.py”,第301行,在uu init中__
self.is_xml=builder.is_xml
AttributeError:'str'对象没有属性'is_xml'
这个代码有什么问题?我试图搜索它,但什么也找不到。我应该在第11行将html更改为xml吗?在这种情况下,它将根据

要解析文档,请将其传递到BeautifulSoup构造函数中。您可以传入字符串或打开的文件句柄:


当使用
BeautifulSoup(text'html','html.parser')
extra'html'违反了BeautifulSoup的构造函数定义时,请参考python3.8/site packages/bs4/__init_;.py中的代码,这会引发错误。因此,按照@FantasqueX在回答部分中的建议,在初始化汤时删除“html”
from bs4 import BeautifulSoup

with open("index.html") as fp:
    soup = BeautifulSoup(fp, 'html.parser')

soup = BeautifulSoup("<html>a web page</html>", 'html.parser')
soup = BeautifulSoup(text, 'html.parser')