网页抓取(Python3)代码中有语法错误吗?
网页抓取(Python3)代码中有语法错误吗?,python,html,css,Python,Html,Css,f.write(产品名称。替换“,”,“;”+”,“+最终价格+”,“+最终评级+”\n”) 此特定行中存在语法错误, 我想制作一个.CSV文件,但产品不在受尊重的文件中。 语法错误为-: 发生异常:UnicodeCodeError “charmap”编解码器无法对位置35中的字符“\u20b9”进行编码:字符映射到 文件“D:\Visual Code Folder\Python\Scraping\u Flipkart.py”,第61行,在 f、 写下(产品名称。替换(“,”,“;”+”,“+最
f.write(产品名称。替换“,”,“;”+”,“+最终价格+”,“+最终评级+”\n”)
此特定行中存在语法错误,
我想制作一个.CSV文件,但产品不在受尊重的文件中。
语法错误为-:
发生异常:UnicodeCodeError
“charmap”编解码器无法对位置35中的字符“\u20b9”进行编码:字符映射到
文件“D:\Visual Code Folder\Python\Scraping\u Flipkart.py”,第61行,在
f、 写下(产品名称。替换(“,”,“;”+”,“+最终价格+”,“+最终评级+”\n”)替换此
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
my_url = 'https://www.flipkart.com/search?q=iphone+12&sid=tyy%2C4io&as=on&as-show=on&otracker=AS_QueryStore_OrganicAutoSuggest_1_6_na_na_na&otracker1=AS_QueryStore_OrganicAutoSuggest_1_6_na_na_na&as-pos=1&as-type=HISTORY&suggestionId=iphone+12%7CMobiles&requestId=71ed5a8e-4348-4fef-9af8-43b7be8c4d83'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
containers = page_soup.findAll("div", {"class": "_13oc-S"})
#print(len(containers)) "will tell number of products on the respected page"
#print(len(containers))
#print(soup.prettify(containers[0])) "will bring the page in the organised manner"
#print(soup.prettify(containers[0]))
container=containers[0]
#print(container.div.img["alt"]) "will display the name of the respected product"
#print(container.div.img["alt"])
#price=container.findAll("div",{"class":"col col-5-12 nlI3QM"}) "will tell the price of the respect project"
price=container.findAll("div",{"class":"col col-5-12 nlI3QM"})
#print(price[0].text)
ratings=container.findAll("div",{"class":"gUuXy-"})
#print(ratings[0].text)
#Making a file
filename="products.csv"
f= open(filename, "w")
#Naming the headers
headers="Product_Name,Pricing,Ratings\n"
f.write(headers)
for container in containers:
product_name = container.div.img["alt"]
price_container = container.findAll("div", {"class": "col col-5-12 nlI3QM"})
price = price_container[0].text.strip()
rating_container = container.findAll("div", {"class":"gUuXy-"})
rating = rating_container[0].text
#print("product_name:" + product_name)
#print("price:" + price)
#print("ratings:" + rating)
#string parsing
trim_price = ''.join(price.split(','))
rm_rupee = trim_price.split("₹")
add_rs_price = "Rs." + rm_rupee[0]
split_price = add_rs_price.split('E')
final_price = split_price[0]
split_rating = rating.split(" ")
final_rating = split_rating[0]
print(product_name.replace(",", "|") + "," + final_price + "," + final_rating + "\n")
f.write(product_name.replace(",", "|") + "," + final_price + "," + final_rating + "\n")
f.close()
用这个
f= open(filename, "w")
使用io可以向后兼容Python 2
如果您只需要支持Python 3,则可以使用内置的open函数:
import io
f = io.open(filename, "w", encoding="utf-8")
向我们显示完整的原始错误消息!请提供预期的价格。我们需要最少的代码,而不是一个50行的程序转储。包括整个错误信息。不接受场外链接和文本图像;我们需要你的问题是独立的,符合本网站的目的。请从中重复和。这将如何修复语法错误?打开文件时,必须使用encoding=“utf-8”对其进行编码。对于Linux平台,不需要这样做,但对于Windows平台,您必须使用它。
with open(fname, "w", encoding="utf-8") as f:
f.write(html)