无法在跨列传播的行中获得结果

时间:2017-11-24 22:23:05

标签: python python-3.x csv web-scraping

我在python中编写了一个脚本来从yelp.com的某个页面获取评论。刮刀正在完美地完成它的工作。结果也成功写入csv文件。该页面提供19条评论。结果我连续19行。但是,我希望将结果改为19个不同的列。

这是网站链接:Link

为了更清楚,结果我喜欢:

1. review
2. review1
3. review2
4. review3

我希望得到的结果:

A        B        C       D
review review1 review2 review3

这是我的剧本:

import requests
import csv
from bs4 import BeautifulSoup

outfile = open("new_file.csv","w",newline="")
writer = csv.writer(outfile)

res = requests.get("use_above_link").text
soup = BeautifulSoup(res,"lxml")
for link in soup.select(".review-content"):
    review = [item.text for item in link.select("p")]
    print(review)
    writer.writerow(review)

2 个答案:

答案 0 :(得分:0)

你必须压缩你的阵列:

import requests
import csv
from bs4 import BeautifulSoup

with open("new_csv_file",'w', newline="",encoding="UTF-8") as doc:
    writer = csv.writer(doc, delimiter=";")

    res = requests.get("use_above_link").text
    soup = BeautifulSoup(res,"lxml")
    for link in soup.select(".review-content"):
        reviews = [item.text for item in link.select("p")]
        z = zip(reviews)
        writer.writerow(z)

答案 1 :(得分:0)

已经有了解决方案。应该是这样的:

import requests
import csv
from bs4 import BeautifulSoup

outfile = open("new_file.csv","w",newline="")
writer = csv.writer(outfile)

res = requests.get("above_link").text
soup = BeautifulSoup(res,"lxml")
review = [item.text for item in soup.select(".review-content p")]
print(review)
writer.writerow(review)