我正在尝试用python和beautifulsoup包解析一个网页。我有控制台在保存之前打印出解析结果。在cmd控制台中解析时,每列中的每个数据都打印得很好。但是当保存到csv时,当遇到逗号时,该行会跳转到下一列。遇到逗号之前的列都很好。我正在使用MS Professional Plus 2010来执行csv数据。
以下是代码:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import requests
from lxml import html
import re
filename = "eng.csv"
f = open(filename, "w")
headers ="abc, def, cdf\n"
f.write(headers)
url = ""
r = requests.get(url, headers = {"User-Agent":"Chrome/56.0.2924.87"})
tree = html.fromstring(r.content)
patternAB= r'ab\s=\s"(.*?)"'
script = tree.xpath('//script[contains(., "ab")]/text()')[0]
eng_name=re.search(patternAB, script).group(1)
script1 = tree.xpath('//script[contains(., "ab")]/text()')[2]
regions=re.search(patternAB, script1).group(1)
......#I use the above code repeatedly with different indexes
f.write(eng_name + ";" + regions + ";" + origins + ";" + "\n")
#I also tried "," as connector but to no avail.
f.close()
知道出了什么问题吗? 提前谢谢。