import requests
from bs4 import BeautifulSoup
import json
import threading
def scrape_page(page):
text = requests.get("https://airportcode.bmcx.com/" + str(page) + "__airportcode/")
res = text.text
soup = BeautifulSoup(res, features="lxml")
arr = soup.findAll(name="table")[0].findAll(name="tr")
for i in arr[2:]:
t = i.findAll(name="td")
v = ""
if t[3].string is not None:
v = t[3].string
with lock:
dic[t[1].string] = v
dic = {}
lock = threading.Lock()
n = 290
threads = []
for i in range(1, n):
thread = threading.Thread(target=scrape_page, args=(i,))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
s = json.dumps(dic, ensure_ascii=False)
print(s)
with open('json.json', 'w') as f:
f.write(s)
世界机场三字码
使用Python进行多线程网页抓取:机场代码数据的自动化提取
最新推荐文章于 2025-07-15 12:47:20 发布
本文介绍了一个Python脚本,利用requests和BeautifulSoup库从机场代码网站抓取数据,通过多线程技术提高效率,最后将数据存储为JSON文件。
356

被折叠的 条评论
为什么被折叠?



