I have an error.
I need to write my output to csv file. But I cant do that, if you have time please help also with pagination. ERROR CODE : TabError: inconsistent use of tabs and spaces in indentation
import requests
import csv
from bs4 import BeautifulSoup as bs
from lxml import etree
url = 'https://bina.az/baki/alqi-satqi/menziller/yeni-tikili/1-otaqli'
r = requests.get(url)
soup = bs(r.content,"lxml")
container = soup.find_all("div",class_="items_list")[-1]
items = container.find_all("div",class_="items-i")
file = open('menziller.csv', 'w',newline='')
writer = csv.writer(file)
writer.writerow(["price", "location", "info"])
for element in items:
price = element.find("div",class_="price").text
location = element.find("div",class_="location").text
info = " ".join([x.text for x in element.find("ul",class_="name").find_all("li")])
writer.writerow([price, location, info])
file.close()
CodePudding user response:
The TabError: inconsistent use of tabs and spaces in indentation error occurs when the file contains a mix of spaces and tabs. I suspect that one of the lines 20-23 use tab(s), while one of the others uses spaces.
Python does not allow a mix between spaces and tabs. This is because on the number of spaces that is equivalent to a tab differs per system, ranging from 2, 4 or 8 spaces. The resulting program would differ depending on how Python converts these tabs back into spaces, which would lead to horribly very confusing bugs.
Instead, Python throws an exception telling you to either exclusively use tabs, or exclusively use spaces. It is customary to use spaces.
CodePudding user response:
This is not valid python syntax. Please use a code editor like VSCode to write your code.
Reference: https://code.visualstudio.com/
You can also use this: https://www.tutorialspoint.com/online_python_formatter.htm
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import csv
from bs4 import BeautifulSoup as bs
from lxml import etree
url = 'https://bina.az/baki/alqi-satqi/menziller/yeni-tikili/1-otaqli'
r = requests.get(url)
soup = bs(r.content, 'lxml')
container = soup.find_all('div', class_='items_list')[-1]
items = container.find_all('div', class_='items-i')
file = open('menziller.csv', 'w', newline='')
writer = csv.writer(file)
writer.writerow(['price', 'location', 'info'])
for element in items:
price = element.find('div', class_='price').text
location = element.find('div', class_='location').text
info = ' '.join([x.text for x in element.find('ul', class_='name'
).find_all('li')])
writer.writerow([price, location, info])
file.close()
Here you go!
