0
我試圖將輸出從webscraper導出到CSV文件。代碼工作正常,當我在終端中運行它時會得到正確的輸出,但它不會傳輸到CSV文件。BeautifulSoup輸出不會傳輸到CSV文件
問題
當我刪除了循環正常工作的第一次,但我想不出到底是什麼錯誤它在這一部分?
代碼
import csv ; import requests
from bs4 import BeautifulSoup
outfile = open('ImplementTest8.csv','w')
writer = csv.writer(outfile)
writer.writerow(["job_link", "job_desc"])
res = requests.get("http://implementconsultinggroup.com/career/#/6257").text
soup = BeautifulSoup(res,"lxml")
links = soup.find_all("a")
for li in soup.find('ul', class_='list-articles list').find_all('li'):
level = li.find_all('dd', {'class': 'author'})[1].get_text()
if "Graduate" in level:
links = li.find_all("href")
for link in links:
if "career" in link.get("href") and 'COPENHAGEN' in link.text:
item_link = link.get("href").strip()
item_text = link.text.replace("View Position","").encode('utf-8').strip()
writer.writerow([item_link, item_text])
print(item_link, item_text)
編輯代碼
import csv ; import requests
from bs4 import BeautifulSoup
outfile = open('ImplementTest8.csv','w')
writer = csv.writer(outfile)
writer.writerow(["job_link", "job_desc"])
res = requests.get("http://implementconsultinggroup.com/career/#/6257").text
soup = BeautifulSoup(res,"lxml")
links = soup.find_all("a")
for li in soup.find('ul', class_='list-articles list').find_all('li'):
level = li.find_all('dd', {'class': 'author'})[1].get_text()
if "Graduate" in level:
links = li.find_all(href=True)
for link in links:
if "career" in link.get("href") and 'COPENHAGEN' in link.text:
item_link = link.get("href").strip()
item_text = link.text.replace("View Position","").encode('utf-8').strip()
writer.writerow([item_link, item_text])
print(item_link, item_text)
感謝TM - 我只是試圖從(HREF)的代碼更改(HREF = TRUE),但它給了我下面的錯誤:文件「」,第4行語法錯誤:關鍵字不能是表達式 –
不要在'href'中使用引號,它是一個參數名稱而不是參數值。值爲'True' –
明白了 - CSV表格仍然是空的。我已經在上面編輯的代碼中添加了。 –