Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 3.x 如何使用csv文件或文本文件(不使用pandas)将web抓取的数据转换为表格格式_Python 3.x_List_Csv_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 3.x 如何使用csv文件或文本文件(不使用pandas)将web抓取的数据转换为表格格式

Python 3.x 如何使用csv文件或文本文件(不使用pandas)将web抓取的数据转换为表格格式,python-3.x,list,csv,web-scraping,beautifulsoup,Python 3.x,List,Csv,Web Scraping,Beautifulsoup,我有一个网站的数据刮,我需要把它变成一个csv文件,然后读取该文件并显示它 请不要使用熊猫,将其转换为数据帧,然后将其转换为csv文件 我想要一种方式,将刮取的数据直接写入csv文件,然后还需要读取csv文件中的数据并在python idle中显示 下面是代码 import requests from bs4 import BeautifulSoup start_url="https://www.indeed.co.in/jobs?q=teacher&l=India"

我有一个网站的数据刮,我需要把它变成一个csv文件,然后读取该文件并显示它

请不要使用熊猫,将其转换为数据帧,然后将其转换为csv文件

我想要一种方式,将刮取的数据直接写入csv文件,然后还需要读取csv文件中的数据并在python idle中显示

下面是代码

import requests
from bs4 import BeautifulSoup

start_url="https://www.indeed.co.in/jobs?q=teacher&l=India"
page_data=requests.get(start_url) #sending a http request to the site
soup=BeautifulSoup(page_data.content,"html.parser") #getting that requested data to store in an object

#lists in which the data is going to be appended
Title=[]
Company=[]
Summary=[]
Location=[]
Link_to_apply=[]
  

for job_tag in soup.find_all("div",class_="jobsearch-SerpJobCard unifiedRow row result"):  

    title=job_tag.find("h2",class_="title")
    company=job_tag.find("span",class_="company")
    location=job_tag.find(class_="location accessible-contrast-color-location").text.strip()
    summary=job_tag.find("div",class_="summary")
    link=job_tag.find("a",href=True)
    base_url="https://www.indeed.com"
    final_link=base_url+link["href"]

   Title.append(title.text.replace('/n'," ").strip())   ###text removes all the unwanted text and gives only the data
   Company.append(company.text.replace('\n'," ").strip())## replace() its replces new lines with just 1 space bar
   Summary.append(summary.text.replace('\n'," ").strip())#strip() replaces all leading and trailing spaces
   Location.append(location.replace('\n'," "))
   Link_to_apply.append(final_link)

请注意,只能使用python idle

您一次问了两个问题。下面的方法应该能够解决这两个问题。它的第一部分将刮取数据写入csv文件,最后一部分从新创建的csv文件中读取数据。您最好将脚本放在一个文件夹中并执行它,以便可以在同一文件夹中获取csv文件

import csv
import requests
from bs4 import BeautifulSoup

base_url = "https://www.indeed.com"
start_url = "https://www.indeed.co.in/jobs?q=teacher&l=India"

page_data = requests.get(start_url)
soup = BeautifulSoup(page_data.content,"html.parser")

with open("output.csv","w",newline="",encoding="utf-8-sig") as f:
    writer = csv.writer(f)
    writer.writerow(['title','company','location','summary','final_link'])
    for job_tag in soup.find_all("div",class_="jobsearch-SerpJobCard"):  
        title = job_tag.find("h2",class_="title").get_text(strip=True)
        company = job_tag.find("span",class_="company").get_text(strip=True)
        location = job_tag.find(class_="location").get_text(strip=True)
        summary = job_tag.find("div",class_="summary").get_text(strip=True)
        link = job_tag.find("a",href=True)
        final_link = base_url + link["href"]
        writer.writerow([title,company,location,summary,final_link])

with open("output.csv","r",encoding="utf-8-sig") as r:
    reader = csv.DictReader(r)
    for item in reader:
        print(item['title'],item['company'])

它给出了一个错误错误被向下回传(最近一次调用是最后一次):文件“C:/Users/sajim/Pictures/New folder/stack overflow try.py”,第21行,在writer.writerow([title,company,location,summary,final_link])中,文件“C:\Users\sajim\Desktop\python official\lib\encodings\cp1252.py”,第19行,在encode return codes.charmap_encode中(输入、自身错误、编码表)[0]UnicodeEncodeError:“charmap”编解码器无法对位置122处的字符“\u20b9”进行编码:字符映射到该位置是因为编码。我已修改了上述脚本。请立即重试。尽管我没有收到错误消息,但现在是否有方法将数据显示为表格列或任何表格格式。输出有点混乱,这就是原因。您知道吗这是脚本应该生成的,除非您手动修改。我知道它在csv文件中的表格格式。我的意思是,有没有一种方法可以让表在空闲时显示而不被截断