此代碼不斷拋出錯誤,我無法讓它發揮作用以保存到文本檔案。它一直卡在
回溯(最后一次呼叫):檔案“c:\Python39\scrape2.py”,第 32 行,回應 = requests.get(url % page, headers=headers).json() 第 918 行,在 json 中
引發 RequestsJSONDecodeError( e.msg, e.doc, e.pos) requests.exceptions.JSONDecodeError: [Errno Expecting value] : 0
import requests
import json
page = 1
url = f"https://api-prod.grip.events/1/container/4368/search?search=&sort=name&order=asc&type_id=4907,4906,5265,4964,4904,1026,4908&page=%d"
headers = {
'authority': 'api-prod.grip.events',
'accept': 'application/json',
'accept-language': 'en-gb',
'content-type': 'application/json',
'if-none-match': 'W/"7132-A/vrxQVW3GqTDiJFLQqx9lN Y0s"',
'login-source': 'web',
'origin': 'https://connect.money2020.com',
'referer': 'https://connect.money2020.com/money2020europe/app/home/network/list/34589',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'cross-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36',
'x-authorization': 'a422cc2a-31fb-4b4e-a1bd-a34b561adc6c',
'x-grip-version': 'Web/8.3.11',
}
s = requests.Session()
response = requests.post(url, headers=headers)
with open("list.txt", "w") as f:
for page in range(1, 1000):
response = requests.get(url % page, headers=headers).json()
contacts = response("data")
for contact in contacts:
target = "%s\t%s\t%s\t%s" % (contact["company_name"], contact["job_title"], contact["name"], contact["job_industry"])
f.write(target "\n")
print(target)
uj5u.com熱心網友回復:
服務器回傳 HTTP 代碼 304(未修改),因為if-not-match標頭已經與 ETag 服務器端匹配(因為這可能是從瀏覽器 devtools 復制的)。
只需去掉這個標題(和其他一些不必要的),并修復錯字(contacts = response["data"])。
import requests
import json
url = "https://api-prod.grip.events/1/container/4368/search?search=&sort=name&order=asc&type_id=4907,4906,5265,4964,4904,1026,4908&page=%d"
headers = {
'x-authorization': 'a422cc2a-31fb-4b4e-a1bd-a34b561adc6c'
}
with open("list.txt", "w") as f:
for page in range(1, 1000):
response = requests.get(url % page, headers=headers).json()
contacts = response["data"]
for contact in contacts:
target = "%s\t%s\t%s\t%s" % (contact["company_name"], contact["job_title"], contact["name"], contact["job_industry"])
f.write(target "\n")
print(target)
您可能還想查看csv用于撰寫 TSV 檔案的模塊。
uj5u.com熱心網友回復:
您確定回應是有效的 json 嗎?它可能會收到錯誤,而您還沒有處理這種情況。
嘗試將其更新為以下內容,它應該會列印任何錯誤。
with open("list.txt", "w") as f:
for page in range(1, 1000):
try:
response = requests.get(url % page, headers=headers)
if response.status_code == 200:
response = response.json()
contacts = response("data")
for contact in contacts:
target = "%s\t%s\t%s\t%s" % (contact["company_name"], contact["job_title"], contact["name"], contact["job_industry"])
f.write(target "\n")
print(target)
else:
print(f"Unsuccessful request: {response}")
except Exception as e:
print(f"Error: {e}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/486869.html
