python爬取文本中的成语_python 关于爬取文本的三个问题

在Python爬虫初学者尝试爬取日文网站文本时,遇到了三个问题:1) 使用BeautifulSoup查找元素时出现AttributeError;2) 输出的内容显示为乱码;3) 更改编码为'shift_jis'后导致UnicodeEncodeError。代码示例中展示了获取评论并写入CSV文件的过程,但在处理非UTF-8编码时遇到困难。
摘要由CSDN通过智能技术生成

学习了python入门,自己练习着想爬取日文网站文本,遇到三个问题一直解决不了。望老师高手给个解答。代码如下

​1.想爬取('div', {'class': 'boxIn clearfix minH'})下“p”的文字,报错内容为:AttributeError: ResultSet object has no attribute 'find_all'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

2.把find_boxin.find_all("p")改成find_boxin[:-1]试了一下,可以输出,但全是乱码。

3.在2的基础上把encoding="utf-8", 改成encoding="shift_jis",

报错为UnicodeEncodeError: 'shift_jis' codec can't encode character '\x83' in position 4: illegal multibyte sequence

菜鸟求教中!!!"""

获取价格网评论

"""

import requests

from bs4 import BeautifulSoup

import csv

def get_comment_div(url):

get_comment_list =

r = requests.get(url, timeout=30)

soup = BeautifulSoup(r.text, "lxml")

find_boxin = soup.find_all('div', {'class': 'boxIn clearfix minH'})

find_comment = find_boxin.find_all("p")

for comment in find_comment:

user_comment = comment.text

get_comment_list.append(user_comment)

return get_comment_list

def main():

"""

主函数

"""

url = "http://bbs.kakaku.com/bbs/-/CategoryCD=6460/"

# 获取评论

get_comment = get_comment_div(url)

with open("get_user_word.csv", "w", encoding="utf-8", newline="")as f:

writer = csv.writer(f)

for i, comment in enumerate(get_comment):

if (i + 1) % 5 == 0:

print("正在读取第{}条数据,一共{}条数据".format(i+1, len(get_comment)))

writer.writerow(comment)

if __name__ == '__main__':

main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值