简体   繁体   English

TypeError:参数1必须可转换为缓冲区,而不是BeautifulSoup

[英]TypeError: argument 1 must be convertible to a buffer, not BeautifulSoup

from bs4 import BeautifulSoup
import requests
import csv


page=requests.get("http://www.gigantti.fi/catalog/tietokoneet/fi_kannettavat/kannettavat-tietokoneet")

data=BeautifulSoup(page.content)

h=open("test.csv","wb+")
h.write(data)
h.close()

print (data)

i have tried running the code as it is without printing it in csv file and it runs perfectly but the moment I try to save it in csv I get the error : argument 1 must be convertible to a buffer, not BeautifulSoup. 我尝试按原样运行代码而不将其打印在csv文件中,并且它可以完美运行,但是当我尝试将其保存在csv中时,出现错误:参数1必须可转换为缓冲区,而不是BeautifulSoup。 PLEASE HELP and thanks in advance 请帮助并预先感谢

I don't know whether someone was able to solve it or not but my hit and trial worked. 我不知道是否有人能够解决它,但我的命中和尝试奏效了。 the problem was I was not converting the content to string. 问题是我没有将内容转换为字符串。

#what i needed to add was:
#after line data=BeautifulSoup(page.content)
a=str(data)

Hopefully this helps 希望这会有所帮助

What you are trying to do doesn't make any sense. 您要尝试执行的操作没有任何意义。

As mentioned on Beautiful Soup Documentation : 如《 美丽汤文档》所述

Beautiful Soup is a Python library for pulling data out of HTML and XML files. Beautiful Soup是一个Python库,用于从HTML和XML文件中提取数据。 It works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. 它与您最喜欢的解析器一起使用,提供了导航,搜索和修改解析树的惯用方式。 It commonly saves programmers hours or days of work. 通常可以节省程序员数小时或数天的工作时间。

You do not seem to be pulling any data but you are trying to write a BeautifulSoup object into a file which doesn't make sense. 您似乎并未提取任何数据,但您正在尝试将BeautifulSoup对象写入没有意义的文件中。

>>> type(data)
<class 'bs4.BeautifulSoup'>

What you should be using BeautifulSoup for is to search the data for some information, and then use that information, here's a useless example: 您应该使用BeautifulSoup的目的是在数据中搜索一些信息,然后使用该信息,这是一个无用的示例:

from bs4 import BeautifulSoup
import requests
page = requests.get("http://www.gigantti.fi/catalog/tietokoneet/fi_kannettavat/kannettavat-tietokoneet")

data = BeautifulSoup(page.content)
with open("test.txt", "wb+") as f:
   # find the first `<title>` tag and retrieve its value 
   value = data.findAll('title')[0].text
   f.write(value)

It seems like you should be using BeautifulSoup to be retreiving all the information on each product in the product listing and putting them into columns in a csv file if I'm guessing correctly, but I will leave that work up to you. 看来您应该使用BeautifulSoup检索产品列表中每个产品的所有信息,如果我猜对了,请将它们放入csv文件的列中,但是我会把这些工作交给您。 You must use BeautifulSoup to find each product in the html and then retrieve all of its details and print to a csv 您必须使用BeautifulSouphtml找到每个产品,然后检索其所有详细信息并打印到csv

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python TypeError:必须可转换为缓冲区,而不是OrderedDict - Python TypeError: must be convertible to a buffer, not OrderedDict 在python中将数据帧打印到.csv。 TypeError:必须转换为缓冲区 - Printing dataframe to .csv in python. TypeError:must be convertible to a buffer 必须可转换为缓冲区,而不是QueryDict Django - must be convertible to a buffer, not QueryDict Django TypeError:参数1必须是字符串或只读字符缓冲区,而不是None - TypeError: argument 1 must be string or read-only character buffer, not None TypeError:Parse()参数1必须是字符串或只读缓冲区,而不是元组 - TypeError: Parse() argument 1 must be string or read-only buffer, not tuple TypeError:query()参数1必须是字符串或只读缓冲区,而不是元组 - TypeError: query() argument 1 must be string or read-only buffer, not tuple TypeError:参数必须是字符串或只读缓冲区,而不是字节数组 - TypeError: argument must be string or read-only buffer, not bytearray Python Beautifulsoup:file.write(str)方法获取TypeError:write()参数必须是str,而不是BeautifulSoup - Python Beautifulsoup : file.write(str) method get TypeError: write() argument must be str, not BeautifulSoup TypeError:带有BeautifulSoup的预期字符串或缓冲区 - TypeError: expected string or buffer with BeautifulSoup python字符串模板:错误“必须可转换为缓冲区,而不是模板” - python string template : Error 'must be convertible to a buffer, not Template'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM