Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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 使用Beautiful Soup从刮取的数据写入CSV文件_Python_Csv_Beautifulsoup - Fatal编程技术网

Python 使用Beautiful Soup从刮取的数据写入CSV文件

Python 使用Beautiful Soup从刮取的数据写入CSV文件,python,csv,beautifulsoup,Python,Csv,Beautifulsoup,这就是我使用Beautifulsoup刮取数据的方式 comments =[] users_list = [] users = driver.find_elements_by_class_name('_6lAjh') for user in users: users_list.append(user.text) i = 0 texts_list = [] texts = driver.find_elements_by_class_name('C4VMK') for txt in t

这就是我使用Beautifulsoup刮取数据的方式

comments =[]
users_list = []
users = driver.find_elements_by_class_name('_6lAjh')

for user in users:
    users_list.append(user.text)

i = 0
texts_list = []
texts = driver.find_elements_by_class_name('C4VMK')

for txt in texts:
    texts_list.append(txt.text.split(users_list[i])[1].replace("\r"," ").replace("\n"," "))
    i += 1
    comments_count = len(users_list)

for i in range(1, comments_count):
    user = users_list[i]
    text = texts_list[i]
    print("User ",user)
    print("Text ",text)
    print()
    comments.append(users_list[i])
    comments.append(texts_list[i])
    idxs = [m.start() for m in re.finditer('@', text)]
    for idx in idxs:
        handle = text[idx:].split(" ")[0]

print(handle)
这是我的文本数据,包括用户名、评论和instagram中的赞数。 “heyyy 3w1 likeReply”->“heyyy”在这里是评论,3w表示评论是在3周前写的,1 like是喜欢的数量

print(comments)
['User1',

“您有一个平面列表,这样您就可以使用
zip
对用户及其评论进行分组

comments=['User1',

'您能给我们一些要运行的示例代码吗?您当前的代码有什么问题?您是如何获得它的?也许您可以更好地使用BeautifulSoup将其作为分离元素。最终使用
split(“”)
rsplit(“”)
进行拆分-但缺少正确操作所需的空格。@EdWard我编辑了它!我不知道它是否有用…@furas我添加了描述我如何获取数据的代码!该数据的url在哪里-创建最小的工作代码以便我们可以运行它。对于当前数据,您必须首先转换它们,然后才能尝试保存到csv中。您必须将文本拆分为列,从
3w1
获取编号(即使用
regex
),等等。