同样的代码,敲了十几次,检查了n次,频繁的出错,然后复制网友的“导入csv”代码,成功了,想哭
运行环境:Python3.6.1
代码:(拉勾网,Python相关招聘信息)
import requests
from bs4 import BeautifulSoup
import csv
headers={"User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36"}
jobs=[]
addrs=[]
moneys=[]
jaxs=[]
coms=[]
def get_inf(url):
html=requests.get(url)
soup=BeautifulSoup(html.text,"lxml")
job=soup.find_all("h2")
addr=soup.find_all("span",{"class":"add"})
money=soup.find_all("span",{"class":"money"})
#jax=
com=soup.find_all("div",{"class":"company_name"})
for one in job:
jobs.append(one.get_text())
for one in addr:
addrs.append(one.get_text())
for one in money:
moneys.append(one.get_text())
# for one in com:
# coms.append(one.get_text())
#get_inf(url)
urls=["https://www.lagou.com/zhaopin/Python/{}/".format(str(i))for i in range(1,30+1)]
for url in urls:
get_inf(url)
with open('E:\credit_test.csv','w',newline="") as file:
w = csv.writer(file)
w.writerow(('工作名称','工作地址','薪酬'))
w.writerows(zip(jobs,addrs,moneys))
导出效果:
碎觉啦